//bit chass 1
#include <bits/stdc++.h>
#define x first
#define y second
#define el "\n"
#define ll long long
#define pb push_back
#define pll pair <ll, ll>
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
#define lcm(x,y) x * y / __gcd(x, y)
#define ibase ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;
const int N = 1e5 + 5, inf = 1e9 + 7, M = 2e6, MM = 2e6 + 5, K = 300;
const ll MI = 2e18;
const double P = 3.14;
void solve() {
int n, k = 0;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++)
if (s[i] == 'R')
k++;
if (k > (n + 1) / 2 || (n - k) > (n + 1) / 2)
cout << -1;
else {
int r = 2;
ll ans = 0;
if (n % 2 == 0) {
for (int i = 1; i < s.size(); i++) {
r = max(r, i + 1);
if (s[i] == s[i - 1]) {
while (s[r] == s[i - 1])
r++;
s.erase(r, 1);
ans += r - i;
}
}
}
else {
if (k == n / 2 + 1) {
r = 0;
while (s[r] != 'R')
r++;
ans += r;
s[0] = 'R';
if (r != 0)
s.erase(r, 1);
}
else {
r = 0;
while (s[r] != 'G')
r++;
ans += r;
s[0] = 'G';
if (r != 0)
s.erase(r, 1);
}
for (int i = 1; i < s.size(); i++) {
r = max(r, i + 1);
if (s[i] == s[i - 1]) {
while (s[r] == s[i])
r++;
s.erase(r, 1);
ans += r - i;
}
}
}
cout << ans;
}
}
int main() {
ibase;
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
// cout << "Case " << i << ": ";
solve();
cout << el;
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 1; i < s.size(); i++) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 1; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |