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;
int R, G, B;
int main() {
int N;
cin >> N;
char str[N];
for (int i = 0; i < N; i++) {
cin >> str[i];
if (str[i] == 'R') {
R++;
} else if (str[i] == 'G') {
G++;
} else {
B++;
}
}
if (max(max(R,G), B) > (N + 1) / 2) {
cout << -1 << endl;
return 0;
}
int l = 1, r = 1;
int moves = 0;
// if same as previous go next until not same
while (l < N) {
if (str[l] == str[l-1]) {
// if (r <= l) r = l + 1;
r = l + 1;
while (str[r] == str[l-1]) {
r++;
}
swap(str[l], str[r]);
moves += r - l;
}
l++;
}
cout << moves << 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... |