#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) x.size()
#define pb push_back
#define F first
#define S second
#define nl '\n'
void solve()
{
int n;
string s;
cin >>n >>s;
int cntr = 0, cntg = 0;
vector< int >r, g;
int pr[n], pg[n];
pr[0] = pg[0] = 0;
for( int i = 0; i < n; ++i )
{
if( i > 0 )
{
pr[i] = pr[i - 1];
pg[i] = pg[i - 1];
}
if( s[i] == 'R' )
{
r.pb( i );
++pr[i];
++cntr;
}
if( s[i] == 'G' )
{
g.pb( i );
++pg[i];
++cntg;
}
}
if( cntg + cntr == n )
{
if( cntg > cntr )
{
swap(cntr, cntg);
swap(r, g);
for( int i = 0; i < n; ++i )
swap(pr[i], pg[i]);
}
if( cntr - cntg > 1 )
{
cout <<-1 <<nl;
return;
}
int ans = 0;
if( cntr > cntg )
{
int pos = 1;
for( int i: g )
{
ans += abs(pos - i);
pos += 2;
}
}
else
{
int cnt1 = 0, cnt2 = 0, pos = 0;
for( int i: r )
{
cnt1 += abs(i - pos);
pos += 2;
}
pos = 0;
for( int i: g )
{
cnt2 += abs(i - pos);
pos += 2;
}
ans = min(cnt1, cnt2);
}
cout <<ans <<nl;
return;
}
else
{
int ans = 0;
while( 1 )
{
int cnt = 0;
for( int i = 1; i < n - 1; ++i )
if( s[i] == s[i - 1] && s[i] != s[i + 1] )
{
swap(s[i], s[i + 1]);
++cnt;
}
if( !cnt )
break;
ans += cnt;
}
while( 1 )
{
int cnt = 0;
for( int i = n - 2; i > 0; --i )
if( s[i] == s[i + 1] && s[i] != s[i - 1] )
{
swap(s[i], s[i - 1]);
++cnt;
}
if( !cnt )
break;
ans += cnt;
}
cout <<ans <<nl;
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T = 1;
// cin >>T;
while( T-- )
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |