This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
signed main(){
int n; cin >> n;
string s; cin >> s;
map<char, vector<int>> pos;
for(int i = 0; i < n; i++)
pos[s[i]].push_back(i);
auto check = [&](string t) -> int{
int res = 0;
map<char, int> ind;
ind['R']=0, ind['G']=0, ind['Y']=0;
for(int i = 0; i < n; i++){
if(i < n-1) if(t[i] == t[i+1]) return 1e9;
res += abs(i-pos[t[i]][ind[t[i]]++]);
}
return res;
};
int ans = 1e9;
sort(begin(s), end(s));
ans = min(ans, check(s));
while(next_permutation(begin(s), end(s))){
ans = min(ans, check(s));
//cout << s << " " << ans << endl;
}
cout << (ans == 1e9 ? -1 : ans/2) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |