#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'
const int N = 2e5;
const int inf = 1e12;
bool ch( string s )
{
for( int i = 1; i < sz(s); ++i )
if( s[i] == s[i - 1] )
return false;
return true;
}
void solve()
{
int n;
string s;
cin >>n >>s;
int x, y, z;
x = y = z = 0;
vector< int >r, g;
for( int i = 0; i < n; ++i )
{
if( s[i] == 'R' )
{
r.pb( i );
++x;
}
if( s[i] == 'G' )
{
g.pb( i );
++y;
}
if( s[i] == 'Y' )
++z;
}
if( x - 1 > y + z || y - 1 > x + z || z - 1 > x + y )
{
cout <<-1 <<nl;
return;
}
if( y + x == n )
{
if( y > x )
{
swap(x, y);
swap(r, g);
}
int ans = 0;
if( x > y )
{
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 = inf;
map<string, int>mp;
queue< string >q;
mp[s] = 0;
q.push(s);
while( !q.empty() )
{
string v = q.front();
q.pop();
if( ch(v) )
{
cout <<mp[v] <<nl;
return;
}
for( int i = 1; i < n; ++i )
{
string v1 = v;
swap(v1[i], v1[i - 1]);
if( !mp.count(v1) )
{
mp[v1] = mp[v] + 1;
q.push(v1);
}
}
}
cout <<-1 <<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();
}
Compilation message
joi2019_ho_t3.cpp: In function 'bool ch(std::string)':
joi2019_ho_t3.cpp:17:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for( int i = 1; i < sz(s); ++i )
| ^
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:89:7: warning: unused variable 'ans' [-Wunused-variable]
89 | int ans = inf;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 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 |
59 ms |
3224 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
7 ms |
848 KB |
Output is correct |
8 |
Correct |
6 ms |
848 KB |
Output is correct |
9 |
Correct |
44 ms |
1964 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
74 ms |
3972 KB |
Output is correct |
12 |
Correct |
24 ms |
1616 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
24 ms |
1428 KB |
Output is correct |
15 |
Correct |
4 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 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 |
59 ms |
3224 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
7 ms |
848 KB |
Output is correct |
8 |
Correct |
6 ms |
848 KB |
Output is correct |
9 |
Correct |
44 ms |
1964 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
74 ms |
3972 KB |
Output is correct |
12 |
Correct |
24 ms |
1616 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
24 ms |
1428 KB |
Output is correct |
15 |
Correct |
4 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
508 KB |
Output is correct |
18 |
Execution timed out |
1064 ms |
169412 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 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 |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
388 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 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 |
59 ms |
3224 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
7 ms |
848 KB |
Output is correct |
8 |
Correct |
6 ms |
848 KB |
Output is correct |
9 |
Correct |
44 ms |
1964 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
74 ms |
3972 KB |
Output is correct |
12 |
Correct |
24 ms |
1616 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
24 ms |
1428 KB |
Output is correct |
15 |
Correct |
4 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
508 KB |
Output is correct |
18 |
Execution timed out |
1064 ms |
169412 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |