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;
#define int long long
typedef pair<int, int> pii;
const int MXN = 405;
int n;
string s;
vector<int> R, G;
int SOLVE0() { // RGRG...RGRGR
int ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += abs(i * 2 + 1 - G[i]);
}
return ans;
}
int SOLVE1() { // RGRG...RGRG, GRGR...GRGR
int ans0 = 0, ans1 = 0;
for (int i = 0; i < n / 2; i++) {
ans0 += abs(i * 2 + 1 - G[i]);
ans1 += abs(i * 2 + 1 - R[i]);
}
return min(ans0, ans1);
}
int SOLVE2() { // GRGR...GRGRG
int ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += abs(i * 2 + 1 - R[i]);
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') R.push_back(i);
else if (s[i] == 'G') G.push_back(i);
else return 0;
}
if (R.size() == G.size() + 1) cout << SOLVE0() << endl;
else if (R.size() == G.size()) cout << SOLVE1() << endl;
else if (R.size() + 1 == G.size()) cout << SOLVE2() << endl;
else cout << -1 << endl;
return 0;
}
# | 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... |