-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e62e7c
commit 320989e
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |