#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 410;
const int inf = 1e8;
int dp[2][maxn][maxn][3];
int idxs[3][maxn];
int ps[maxn][3];
int cnt[3];
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
string _s; cin >> _s;
vector<int> s;
for(int i = 0; i < n; i++)
{
char ch = _s[i];
if(ch == 'R')
s.push_back(0);
else if(ch == 'G')
s.push_back(1);
else
s.push_back(2);
idxs[s.back()][cnt[s.back()]++] = i;
for(int j = 0; j < 3; j++)
ps[i+1][j] = ps[i][j];
ps[i+1][s.back()]++;
}
if(2*max(cnt[0], max(cnt[1], cnt[2])) > n+1)
{
cout << -1 << endl;
return 0;
}
//for(int i = 0; i <= n; i++)
// //cerr << ps[i][0] << " " << ps[i][1] << " " << ps[i][2] << endl;
for(int x = 0; x < 2; x++)
for(int y = 0; y <= cnt[1]; y++)
for(int z = 0; z <= cnt[2]; z++)
for(int c = 0; c < 3; c++)
dp[x][y][z][c] = inf;
for(int x = 0; x <= cnt[0]; x++)
{
for(int y = 0; y <= cnt[1]; y++)
for(int z = 0; z <= cnt[2]; z++)
for(int c = 0; c < 3; c++)
dp[x&1][y][z][c] = inf;
for(int y = 0; y <= cnt[1]; y++)
{
for(int z = 0; z <= cnt[2]; z++)
{
if(x == 0 and y == 0 and z == 0)
{
dp[x&1][y][z][0] = 0;
dp[x&1][y][z][1] = 0;
dp[x&1][y][z][2] = 0;
continue;
}
if(x > 0)
{
int i = idxs[0][x-1];
int a = i - min(ps[i][0],x) - min(ps[i][1],y) - min(ps[i][2],z);
dp[x&1][y][z][0] = min(dp[!(x&1)][y][z][1], dp[!(x&1)][y][z][2]) + a;
////cerr << a << " ";
}
if(y > 0)
{
int i = idxs[1][y-1];
int a = i - min(ps[i][0],x) - min(ps[i][1],y) - min(ps[i][2],z);
dp[x&1][y][z][1] = min(dp[x&1][y-1][z][0], dp[x&1][y-1][z][2]) + a;
//cerr << a << " ";
}
if(z > 0)
{
int i = idxs[2][z-1];
int a = i - min(ps[i][0],x) - min(ps[i][1],y) - min(ps[i][2],z);
dp[x&1][y][z][2] = min(dp[x&1][y][z-1][0], dp[x&1][y][z-1][1]) + a;
//cerr << a << " ";
}
//cerr << endl;
//cerr << x << "," << y << "," << z << ": " << dp[x&1][y][z][0] << " " << dp[x&1][y][z][1] << " " << dp[x&1][y][z][2] << endl;
}
}
}
int ans = min(dp[cnt[0]&1][cnt[1]][cnt[2]][0], min(dp[cnt[0]&1][cnt[1]][cnt[2]][1], dp[cnt[0]&1][cnt[1]][cnt[2]][2]));
if(ans >= inf)
ans = -1;
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1864 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1612 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
1876 KB |
Output is correct |
44 |
Correct |
2 ms |
1876 KB |
Output is correct |
45 |
Correct |
2 ms |
1876 KB |
Output is correct |
46 |
Correct |
2 ms |
1876 KB |
Output is correct |
47 |
Correct |
2 ms |
1876 KB |
Output is correct |
48 |
Correct |
2 ms |
1864 KB |
Output is correct |
49 |
Correct |
2 ms |
1876 KB |
Output is correct |
50 |
Correct |
1 ms |
1876 KB |
Output is correct |
51 |
Correct |
1 ms |
1876 KB |
Output is correct |
52 |
Correct |
1 ms |
1876 KB |
Output is correct |
53 |
Correct |
1 ms |
1108 KB |
Output is correct |
54 |
Correct |
2 ms |
1364 KB |
Output is correct |
55 |
Correct |
2 ms |
1612 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
29 ms |
1556 KB |
Output is correct |
59 |
Correct |
38 ms |
1620 KB |
Output is correct |
60 |
Correct |
33 ms |
1484 KB |
Output is correct |
61 |
Correct |
38 ms |
1492 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
5 ms |
1876 KB |
Output is correct |
64 |
Correct |
15 ms |
1876 KB |
Output is correct |
65 |
Correct |
22 ms |
2004 KB |
Output is correct |
66 |
Correct |
28 ms |
1480 KB |
Output is correct |
67 |
Correct |
33 ms |
1748 KB |
Output is correct |
68 |
Correct |
31 ms |
1620 KB |
Output is correct |
69 |
Correct |
30 ms |
1492 KB |
Output is correct |
70 |
Correct |
29 ms |
1612 KB |
Output is correct |
71 |
Correct |
27 ms |
1364 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
332 KB |
Output is correct |