/*
* imaN
* 49
*
**/
#include <bits/stdc++.h>
using namespace std;
//#define ll long long
#define FF first
#define SS second
#define pb push_back
#define bp pop_back()
#define int long long
#define ld long double
#define pf push_front
#define Imansoska ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define endl cout << "\n"
#define all(x) x.begin(),x.end()
#define lb lower_bound
/*int binpow (int a, int n, int m) {
if (n == 0)
return 1%m;
if (n % 2 == 1)
return binpow (a, n-1,m) * a%m;
else {
int b = binpow (a, n/2,m);
return b * b%m;
}
}
int lcm(int a, int b) {
return (a / __gcd(a, b)) * b;
}*/
const int N = (1e5+13);
const int maxn= 1e6+100;
const int MOD = 1e9 + 7;
const int INF = 1e18 + 7;
char c[410];
signed main (/*Imansoska*/) {
// Imansoska// freopen("lepus.in","r",stdin);
// freopen("lepus.out","w",stdout);
int n;
cin >> n;
int r=0,g=0;
for ( int i=1; i<=n; i++ ) {
cin >> c[i];
if ( c[i]=='R' ) {
r++;
}
if ( c[i]=='G' ) {
g++;
}
}
if ( abs(r-g)>1 ) {
cout << -1;
return 0;
}
if ( r==g ) {
int cg=0,cr=0,ans=0,anss=0;
for ( int i=1; i<=n; i++ ) {
if ( c[i]=='R' ) {
cr++;
ans+=abs((cr*2-1)-i);
}
if ( c[i]=='G' ) {
cg++;
ans+=abs((cg*2)-i);
}
}
for ( int i=1; i<=n; i++ ) {
if ( c[i]=='R' ) {
cr++;
anss+=abs((cr*2)-i);
}
if ( c[i]=='G' ) {
cg++;
anss+=abs((cg*2-1)-i);
}
}
cout << min(ans,anss);
}
else {
int cg=0,cr=0,ans=0,anss=0;
if ( r>g ) {
for ( int i=1; i<=n; i++ ) {
if ( c[i]=='R' ) {
cr++;
ans+=abs((cr*2-1)-i);
}
if ( c[i]=='G' ) {
cg++;
ans+=abs((cg*2)-i);
}
}
}
else {
for ( int i=1; i<=n; i++ ) {
if ( c[i]=='R' ) {
cr++;
anss+=abs((cr*2)-i);
}
if ( c[i]=='G' ) {
cg++;
anss+=abs((cg*2-1)-i);
}
}
}
cout << min(ans,anss);
}
}
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |