Skip to content

Commit

Permalink
Adding some question form PA
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 committed Dec 20, 2024
1 parent 3e62e7c commit 320989e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
44 changes: 44 additions & 0 deletions projeto_algo/divider_and_conquer/q1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 1490 - D. Permutation Transformation

#include <bits/stdc++.h>
using namespace std;

unordered_map<int,int> unmap;

void getRootSubArray(int l, int r, vector<int> vals, int depth) {

if(l > r) return;

int maxVal = l;
for(int i = l; i <= r; ++i)
if(vals[i] > vals[maxVal]) maxVal = i;

unmap[vals[maxVal]] = depth;

getRootSubArray(l, maxVal - 1, vals, depth+1);
getRootSubArray(maxVal + 1, r, vals, depth+1);

}


int main(){

int t, n;
cin >> t;
while(t--) {
cin >> n;
vector<int> vals(n);
for(int i = 0; i < n; ++i)
cin >> vals[i];

getRootSubArray(0, n - 1, vals, 0);
for(int i = 0; i < n; ++i)
cout << unmap[vals[i]] << " ";
cout << endl;

unmap.clear();
}

return EXIT_SUCCESS;

}
56 changes: 56 additions & 0 deletions projeto_algo/divider_and_conquer/q2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 1385 - D. a-Good String

#include <bits/stdc++.h>
using namespace std;

int makeGoodString(string &s, char c) {
int n = s.size();

if (n == 1) {
return (s[0] != c);
}

int half = n / 2;
string left = s.substr(0, half);
string right = s.substr(half);

int leftChanges = 0;
for (char ch : left) {
if (ch != c) {
leftChanges++;
}
}

int rightChanges = makeGoodString(right, c + 1);

int option1 = leftChanges + rightChanges;

int rightLeftChanges = 0;
for (char ch : right) {
if (ch != c) {
rightLeftChanges++;
}
}

int leftRightChanges = makeGoodString(left, c + 1);

int option2 = rightLeftChanges + leftRightChanges;

return min(option1, option2);
}

int main() {
int t;
cin >> t;

while (t--) {
int n;
string s;
cin >> n >> s;

int result = makeGoodString(s, 'a');
cout << result << endl;
}

return 0;
}
13 changes: 13 additions & 0 deletions projeto_algo/divider_and_conquer/teste.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac

0 comments on commit 320989e

Please sign in to comment.