/** Im the best because i work as hard as i possibly can **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl "\n"
const int N = 4e2 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 5e8;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
/// dp[tp][i][j][k] -> age
char C[N];
string s;
int n, cnt[3], nxt[3][N][N][N], dp[3][N][N][N];
vector < int > pos[3];
inline int Less(int type, int x, int used)
{
return lower_bound(pos[type].begin() + used, pos[type].end(), x) - pos[type].begin() - used;
}
int main()
{
scanf("%d", &n);
scanf("%s", C);
s = C; s = "#" + s;
for(int i = 1; i <= n; i ++)
{
if(s[i] == 'R') s[i] = '0';
else if(s[i] == 'G') s[i] = '1';
else s[i] = '2';
cnt[s[i] - '0'] ++;
pos[s[i] - '0'].push_back(i);
}
for(int i = 0; i <= cnt[0]; i ++)
{
for(int j = 0; j <= cnt[1]; j ++)
{
for(int k = 0; k <= cnt[2]; k ++)
{
if(i != cnt[0])
{
int id = pos[0][i];
nxt[0][i][j][k] = Less(1, id, j) + Less(2, id, k);
}
else nxt[0][i][j][k] = inf;
if(j != cnt[1])
{
int id = pos[1][j];
nxt[1][i][j][k] = Less(0, id, i) + Less(2, id, k);
}
else nxt[1][i][j][k] = inf;
if(k != cnt[2])
{
int id = pos[2][k];
nxt[2][i][j][k] = Less(0, id, i) + Less(1, id, j);
}
else nxt[2][i][j][k] = inf;
///printf("i = %d j = %d k = %d nxt[0] = %d nxt[1] = %d nxt[2] = %d\n", i, j, k, nxt[0][i][j][k], nxt[1][i][j][k], nxt[2][i][j][k]);
}
}
}
///printf("\n\n");
for(int i = 0; i < N; i ++) for(int j = 0; j < N; j ++) dp[0][0][i][j] = dp[1][0][i][j] = dp[2][0][i][j] = inf;
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
for(int i = 1; i <= n; i ++)
{
for(int j = 0; j <= min(i, cnt[0]); j ++)
{
for(int k = 0; k <= min(i - j, cnt[1]); k ++)
{
dp[0][i][j][k] = dp[1][i][j][k] = dp[2][i][j][k] = inf;
if(i - j - k > cnt[2])
{
continue;
}
if(j) dp[0][i][j][k] = min(dp[1][i - 1][j - 1][k], dp[2][i - 1][j - 1][k]) + nxt[0][j - 1][k][i - j - k];
if(k) dp[1][i][j][k] = min(dp[0][i - 1][j][k - 1], dp[2][i - 1][j][k - 1]) + nxt[1][j][k - 1][i - j - k];
if(i != j + k) dp[2][i][j][k] = min(dp[0][i - 1][j][k], dp[1][i - 1][j][k]) + nxt[2][j][k][i - j - k - 1];
}
}
}
/*for(int i = 1; i <= n; i ++)
{
for(int j = 0; j <= cnt[0]; j ++)
{
for(int k = 0; k <= cnt[1]; k ++)
{
printf("i = %d j = %d k = %d dp[0] = %d dp[1] = %d dp[2] = %d\n", i, j, k, dp[0][i][j][k], dp[1][i][j][k], dp[2][i][j][k]);
}
}
}
*/
int ans = min({dp[0][n][cnt[0]][cnt[1]], dp[1][n][cnt[0]][cnt[1]], dp[2][n][cnt[0]][cnt[1]]});
if(ans >= inf) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
44 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
45 | scanf("%s", C);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
3052 KB |
Output is correct |
6 |
Correct |
2 ms |
3180 KB |
Output is correct |
7 |
Correct |
2 ms |
3180 KB |
Output is correct |
8 |
Correct |
2 ms |
3180 KB |
Output is correct |
9 |
Correct |
2 ms |
3308 KB |
Output is correct |
10 |
Correct |
2 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
3052 KB |
Output is correct |
13 |
Correct |
2 ms |
3072 KB |
Output is correct |
14 |
Correct |
2 ms |
3180 KB |
Output is correct |
15 |
Correct |
2 ms |
2924 KB |
Output is correct |
16 |
Correct |
2 ms |
3308 KB |
Output is correct |
17 |
Correct |
2 ms |
2540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
3052 KB |
Output is correct |
6 |
Correct |
2 ms |
3180 KB |
Output is correct |
7 |
Correct |
2 ms |
3180 KB |
Output is correct |
8 |
Correct |
2 ms |
3180 KB |
Output is correct |
9 |
Correct |
2 ms |
3308 KB |
Output is correct |
10 |
Correct |
2 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
3052 KB |
Output is correct |
13 |
Correct |
2 ms |
3072 KB |
Output is correct |
14 |
Correct |
2 ms |
3180 KB |
Output is correct |
15 |
Correct |
2 ms |
2924 KB |
Output is correct |
16 |
Correct |
2 ms |
3308 KB |
Output is correct |
17 |
Correct |
2 ms |
2540 KB |
Output is correct |
18 |
Correct |
8 ms |
11136 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
7 ms |
11500 KB |
Output is correct |
21 |
Correct |
7 ms |
10732 KB |
Output is correct |
22 |
Correct |
7 ms |
10604 KB |
Output is correct |
23 |
Correct |
6 ms |
9708 KB |
Output is correct |
24 |
Correct |
5 ms |
7532 KB |
Output is correct |
25 |
Correct |
9 ms |
15340 KB |
Output is correct |
26 |
Correct |
9 ms |
14976 KB |
Output is correct |
27 |
Correct |
9 ms |
12908 KB |
Output is correct |
28 |
Correct |
6 ms |
10732 KB |
Output is correct |
29 |
Correct |
6 ms |
10860 KB |
Output is correct |
30 |
Correct |
6 ms |
9836 KB |
Output is correct |
31 |
Correct |
6 ms |
9068 KB |
Output is correct |
32 |
Correct |
6 ms |
10220 KB |
Output is correct |
33 |
Correct |
8 ms |
13312 KB |
Output is correct |
34 |
Correct |
7 ms |
12396 KB |
Output is correct |
35 |
Correct |
7 ms |
11372 KB |
Output is correct |
36 |
Correct |
6 ms |
9964 KB |
Output is correct |
37 |
Correct |
5 ms |
8172 KB |
Output is correct |
38 |
Correct |
6 ms |
9068 KB |
Output is correct |
39 |
Correct |
7 ms |
10348 KB |
Output is correct |
40 |
Correct |
2 ms |
3308 KB |
Output is correct |
41 |
Correct |
5 ms |
8044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2412 KB |
Output is correct |
2 |
Correct |
287 ms |
493932 KB |
Output is correct |
3 |
Correct |
282 ms |
492140 KB |
Output is correct |
4 |
Correct |
283 ms |
493932 KB |
Output is correct |
5 |
Correct |
282 ms |
494036 KB |
Output is correct |
6 |
Correct |
281 ms |
494060 KB |
Output is correct |
7 |
Correct |
293 ms |
491244 KB |
Output is correct |
8 |
Correct |
288 ms |
491136 KB |
Output is correct |
9 |
Correct |
279 ms |
489164 KB |
Output is correct |
10 |
Correct |
279 ms |
493932 KB |
Output is correct |
11 |
Correct |
306 ms |
494024 KB |
Output is correct |
12 |
Correct |
77 ms |
135660 KB |
Output is correct |
13 |
Correct |
131 ms |
235372 KB |
Output is correct |
14 |
Correct |
189 ms |
338668 KB |
Output is correct |
15 |
Correct |
284 ms |
493036 KB |
Output is correct |
16 |
Correct |
281 ms |
493164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2412 KB |
Output is correct |
2 |
Correct |
2 ms |
2412 KB |
Output is correct |
3 |
Correct |
2 ms |
2412 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
3052 KB |
Output is correct |
6 |
Correct |
2 ms |
3180 KB |
Output is correct |
7 |
Correct |
2 ms |
3180 KB |
Output is correct |
8 |
Correct |
2 ms |
3180 KB |
Output is correct |
9 |
Correct |
2 ms |
3308 KB |
Output is correct |
10 |
Correct |
2 ms |
3436 KB |
Output is correct |
11 |
Correct |
2 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
3052 KB |
Output is correct |
13 |
Correct |
2 ms |
3072 KB |
Output is correct |
14 |
Correct |
2 ms |
3180 KB |
Output is correct |
15 |
Correct |
2 ms |
2924 KB |
Output is correct |
16 |
Correct |
2 ms |
3308 KB |
Output is correct |
17 |
Correct |
2 ms |
2540 KB |
Output is correct |
18 |
Correct |
8 ms |
11136 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
7 ms |
11500 KB |
Output is correct |
21 |
Correct |
7 ms |
10732 KB |
Output is correct |
22 |
Correct |
7 ms |
10604 KB |
Output is correct |
23 |
Correct |
6 ms |
9708 KB |
Output is correct |
24 |
Correct |
5 ms |
7532 KB |
Output is correct |
25 |
Correct |
9 ms |
15340 KB |
Output is correct |
26 |
Correct |
9 ms |
14976 KB |
Output is correct |
27 |
Correct |
9 ms |
12908 KB |
Output is correct |
28 |
Correct |
6 ms |
10732 KB |
Output is correct |
29 |
Correct |
6 ms |
10860 KB |
Output is correct |
30 |
Correct |
6 ms |
9836 KB |
Output is correct |
31 |
Correct |
6 ms |
9068 KB |
Output is correct |
32 |
Correct |
6 ms |
10220 KB |
Output is correct |
33 |
Correct |
8 ms |
13312 KB |
Output is correct |
34 |
Correct |
7 ms |
12396 KB |
Output is correct |
35 |
Correct |
7 ms |
11372 KB |
Output is correct |
36 |
Correct |
6 ms |
9964 KB |
Output is correct |
37 |
Correct |
5 ms |
8172 KB |
Output is correct |
38 |
Correct |
6 ms |
9068 KB |
Output is correct |
39 |
Correct |
7 ms |
10348 KB |
Output is correct |
40 |
Correct |
2 ms |
3308 KB |
Output is correct |
41 |
Correct |
5 ms |
8044 KB |
Output is correct |
42 |
Correct |
2 ms |
2412 KB |
Output is correct |
43 |
Correct |
287 ms |
493932 KB |
Output is correct |
44 |
Correct |
282 ms |
492140 KB |
Output is correct |
45 |
Correct |
283 ms |
493932 KB |
Output is correct |
46 |
Correct |
282 ms |
494036 KB |
Output is correct |
47 |
Correct |
281 ms |
494060 KB |
Output is correct |
48 |
Correct |
293 ms |
491244 KB |
Output is correct |
49 |
Correct |
288 ms |
491136 KB |
Output is correct |
50 |
Correct |
279 ms |
489164 KB |
Output is correct |
51 |
Correct |
279 ms |
493932 KB |
Output is correct |
52 |
Correct |
306 ms |
494024 KB |
Output is correct |
53 |
Correct |
77 ms |
135660 KB |
Output is correct |
54 |
Correct |
131 ms |
235372 KB |
Output is correct |
55 |
Correct |
189 ms |
338668 KB |
Output is correct |
56 |
Correct |
284 ms |
493036 KB |
Output is correct |
57 |
Correct |
281 ms |
493164 KB |
Output is correct |
58 |
Correct |
434 ms |
287212 KB |
Output is correct |
59 |
Correct |
494 ms |
334956 KB |
Output is correct |
60 |
Correct |
464 ms |
322264 KB |
Output is correct |
61 |
Correct |
447 ms |
308460 KB |
Output is correct |
62 |
Correct |
333 ms |
502252 KB |
Output is correct |
63 |
Correct |
299 ms |
481516 KB |
Output is correct |
64 |
Correct |
371 ms |
449548 KB |
Output is correct |
65 |
Correct |
420 ms |
396780 KB |
Output is correct |
66 |
Correct |
343 ms |
312272 KB |
Output is correct |
67 |
Correct |
308 ms |
278892 KB |
Output is correct |
68 |
Correct |
355 ms |
316396 KB |
Output is correct |
69 |
Correct |
371 ms |
313580 KB |
Output is correct |
70 |
Correct |
402 ms |
319596 KB |
Output is correct |
71 |
Correct |
451 ms |
329964 KB |
Output is correct |
72 |
Correct |
303 ms |
413036 KB |
Output is correct |
73 |
Correct |
22 ms |
19308 KB |
Output is correct |