//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
int change(char c){
if (c == 'R') return 0;
if (c == 'G') return 1;
return 2;
}
void solve(){
int n; cin >> n;
string s; cin >> s;
s = "$" + s;
vector<int>cnt(3);
for (int i = 1; i<=n; i++) cnt[change(s[i])]++;
int c = (n+1)/2;
for (auto x: cnt){
if (x > c){
cout << "-1\n";
return;
}
}
int ans = 0;
cnt[change(s[1])]--;
for (int i = 2; i<=n; i++){
debug(i, s);
bool ok2 = 1;
auto tcnt = cnt;
tcnt[change(s[i])]--;
for (auto x: tcnt){
if (x > (n-i+1)/2){
ok2 = 0;
}
}
if (s[i] == s[i-1] || !ok2){
debug(2137);
bool any = 0;
//musimy zmienic i-ta litere
for (int j = i+1; j<=n; j++){
if (s[j] == s[i]) continue;
auto ncnt = cnt;
debug(cnt);
// ncnt[change(s[i])]++;
ncnt[change(s[j])]--;
debug(ncnt);
bool ok = 1;
for (auto x: ncnt){
if (x > (n-i+1)/2){
ok = 0;
break;
}
}
debug(j, ok);
if (ok){
string t;
for (int rep = 0; rep<i; rep++) t += s[rep];
t += s[j];
ans += (j-i);
for (int rep = i; rep <=n; rep++){
if (rep == j) continue;
t += s[rep];
}
any = 1;
swap(s, t);
break;
}
}
}
cnt[change(s[i])]--;
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:65:18: warning: variable 'any' set but not used [-Wunused-but-set-variable]
65 | bool any = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |