#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define infl 0x3f3f3f3f3f3f3f3f
#define ll long long
int main()
{
int n; string s; cin>>n>>s;
int rc = count(all(s),'R');
int gc = count(all(s),'G');
int yc = count(all(s),'Y');
if(0)bad:{printf("-1\n");return 0;}
if(yc > rc+gc+1) goto bad;
if(gc > rc+yc+1) goto bad;
if(rc > yc+gc+1) goto bad;
assert(yc==0);
string sr(n,'R'),sg(n,'G');
for(int i=1;i<n;i+=2) sr[i]='G',sg[i]='R';
auto solve = [&](string t) -> ll {
if(count(all(t),'R')!=rc) return infl;
if(count(all(t),'G')!=gc) return infl;
int ri=0,gi=0;
while(ri<n&&s[ri]!='R')ri++;
while(gi<n&&s[gi]!='G')gi++;
ll p=0;
for(int i=0;i<n;i++) {
//printf("find %d: ri=%d gi=%d\n",i,ri,gi);
if(t[i] == 'R') {
p += abs(i-ri);
ri++; while(ri<n&&s[ri]!='R')ri++;
}
else {
p += abs(i-gi);
gi++; while(gi<n&&s[gi]!='G')gi++;
}
}
//cout<<s<<' '<<t<<' '<<p<<endl;
return p/2;
};
ll mn= infl;
mn=min(mn,solve(sr));
mn=min(mn,solve(sg));
if(mn == infl) goto bad;
printf("%lld\n",mn);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |