#include <bits/stdc++.h>
using namespace std;
vector<int> wut[3];
int pref[401][3];
int dp[401][401][401][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
for(int i = 0; i <= n; i++) {
pref[i][0] = 0;
pref[i][1] = 0;
pref[i][2] = 0;
}
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= n; k++) {
dp[i][j][k][0] = INT_MAX/2;
dp[i][j][k][1] = INT_MAX/2;
dp[i][j][k][2] = INT_MAX/2;
}
}
}
for(int i = 1; i <= n; i++) {
int c;
if(s[i-1] == 'R') {
c = 0;
}
else if(s[i-1] == 'G') {
c = 1;
}
else {
c = 2;
}
pref[i][c]++;
wut[c].push_back(i);
}
for(int i = 1; i <= n; i++) {
pref[i][0]+=pref[i-1][0];
pref[i][1]+=pref[i-1][1];
pref[i][2]+=pref[i-1][2];
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int ans = INT_MAX;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= n; k++) {
if(i+j+k == 0 || i+j+k > n) {
continue;
}
if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
continue;
}
dp[i][j][k][0] = INT_MAX/2;
dp[i][j][k][1] = INT_MAX/2;
dp[i][j][k][2] = INT_MAX/2;
int y = i+j+k;
if(i > 0) {
int p = wut[0][i-1];
int c = p;
p+=max(0,j-pref[c][1]);
p+=max(0,k-pref[c][2]);
dp[i][j][k][0] = min(dp[i][j][k][0],dp[i-1][j][k][1]+p-y);
dp[i][j][k][0] = min(dp[i][j][k][0],dp[i-1][j][k][2]+p-y);
}
if(j > 0) {
int p = wut[1][j-1];
int c = p;
p+=max(0,i-pref[c][0]);
p+=max(0,k-pref[c][2]);
dp[i][j][k][1] = min(dp[i][j][k][1],dp[i][j-1][k][0]+p-y);
dp[i][j][k][1] = min(dp[i][j][k][1],dp[i][j-1][k][2]+p-y);
}
if(k > 0) {
int p = wut[2][k-1];
int c = p;
p+=max(0,i-pref[c][0]);
p+=max(0,j-pref[c][1]);
dp[i][j][k][2] = min(dp[i][j][k][2],dp[i][j][k-1][0]+p-y);
dp[i][j][k][2] = min(dp[i][j][k][2],dp[i][j][k-1][1]+p-y);
}
if(i+j+k == n) {
ans = min(ans,dp[i][j][k][0]);
ans = min(ans,dp[i][j][k][1]);
ans = min(ans,dp[i][j][k][2]);
}
}
}
}
if(ans > n*n) {
cout << -1;
}
else {
cout << ans;
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:60:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:60:64: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(i > wut[0].size() || j > wut[1].size() || k > wut[2].size()) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1400 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
0 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1400 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
0 ms |
1372 KB |
Output is correct |
18 |
Correct |
7 ms |
18012 KB |
Output is correct |
19 |
Correct |
6 ms |
18012 KB |
Output is correct |
20 |
Correct |
7 ms |
18012 KB |
Output is correct |
21 |
Correct |
6 ms |
18012 KB |
Output is correct |
22 |
Correct |
8 ms |
18012 KB |
Output is correct |
23 |
Correct |
7 ms |
18012 KB |
Output is correct |
24 |
Correct |
7 ms |
18160 KB |
Output is correct |
25 |
Correct |
6 ms |
18056 KB |
Output is correct |
26 |
Correct |
6 ms |
18012 KB |
Output is correct |
27 |
Correct |
7 ms |
18008 KB |
Output is correct |
28 |
Correct |
7 ms |
18008 KB |
Output is correct |
29 |
Correct |
6 ms |
18136 KB |
Output is correct |
30 |
Correct |
7 ms |
18008 KB |
Output is correct |
31 |
Correct |
7 ms |
18008 KB |
Output is correct |
32 |
Correct |
8 ms |
18012 KB |
Output is correct |
33 |
Correct |
6 ms |
17500 KB |
Output is correct |
34 |
Correct |
7 ms |
17500 KB |
Output is correct |
35 |
Correct |
6 ms |
16796 KB |
Output is correct |
36 |
Correct |
6 ms |
17500 KB |
Output is correct |
37 |
Correct |
6 ms |
16220 KB |
Output is correct |
38 |
Correct |
6 ms |
18072 KB |
Output is correct |
39 |
Correct |
7 ms |
18012 KB |
Output is correct |
40 |
Correct |
6 ms |
17460 KB |
Output is correct |
41 |
Correct |
7 ms |
18012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
316 ms |
757320 KB |
Output is correct |
3 |
Correct |
317 ms |
755300 KB |
Output is correct |
4 |
Correct |
300 ms |
757328 KB |
Output is correct |
5 |
Correct |
313 ms |
757396 KB |
Output is correct |
6 |
Correct |
337 ms |
757564 KB |
Output is correct |
7 |
Correct |
313 ms |
755624 KB |
Output is correct |
8 |
Correct |
312 ms |
755284 KB |
Output is correct |
9 |
Correct |
328 ms |
751624 KB |
Output is correct |
10 |
Correct |
329 ms |
757552 KB |
Output is correct |
11 |
Correct |
325 ms |
757460 KB |
Output is correct |
12 |
Correct |
76 ms |
202840 KB |
Output is correct |
13 |
Correct |
136 ms |
357456 KB |
Output is correct |
14 |
Correct |
215 ms |
517556 KB |
Output is correct |
15 |
Correct |
325 ms |
757332 KB |
Output is correct |
16 |
Correct |
310 ms |
757456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1400 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
0 ms |
1372 KB |
Output is correct |
18 |
Correct |
7 ms |
18012 KB |
Output is correct |
19 |
Correct |
6 ms |
18012 KB |
Output is correct |
20 |
Correct |
7 ms |
18012 KB |
Output is correct |
21 |
Correct |
6 ms |
18012 KB |
Output is correct |
22 |
Correct |
8 ms |
18012 KB |
Output is correct |
23 |
Correct |
7 ms |
18012 KB |
Output is correct |
24 |
Correct |
7 ms |
18160 KB |
Output is correct |
25 |
Correct |
6 ms |
18056 KB |
Output is correct |
26 |
Correct |
6 ms |
18012 KB |
Output is correct |
27 |
Correct |
7 ms |
18008 KB |
Output is correct |
28 |
Correct |
7 ms |
18008 KB |
Output is correct |
29 |
Correct |
6 ms |
18136 KB |
Output is correct |
30 |
Correct |
7 ms |
18008 KB |
Output is correct |
31 |
Correct |
7 ms |
18008 KB |
Output is correct |
32 |
Correct |
8 ms |
18012 KB |
Output is correct |
33 |
Correct |
6 ms |
17500 KB |
Output is correct |
34 |
Correct |
7 ms |
17500 KB |
Output is correct |
35 |
Correct |
6 ms |
16796 KB |
Output is correct |
36 |
Correct |
6 ms |
17500 KB |
Output is correct |
37 |
Correct |
6 ms |
16220 KB |
Output is correct |
38 |
Correct |
6 ms |
18072 KB |
Output is correct |
39 |
Correct |
7 ms |
18012 KB |
Output is correct |
40 |
Correct |
6 ms |
17460 KB |
Output is correct |
41 |
Correct |
7 ms |
18012 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
316 ms |
757320 KB |
Output is correct |
44 |
Correct |
317 ms |
755300 KB |
Output is correct |
45 |
Correct |
300 ms |
757328 KB |
Output is correct |
46 |
Correct |
313 ms |
757396 KB |
Output is correct |
47 |
Correct |
337 ms |
757564 KB |
Output is correct |
48 |
Correct |
313 ms |
755624 KB |
Output is correct |
49 |
Correct |
312 ms |
755284 KB |
Output is correct |
50 |
Correct |
328 ms |
751624 KB |
Output is correct |
51 |
Correct |
329 ms |
757552 KB |
Output is correct |
52 |
Correct |
325 ms |
757460 KB |
Output is correct |
53 |
Correct |
76 ms |
202840 KB |
Output is correct |
54 |
Correct |
136 ms |
357456 KB |
Output is correct |
55 |
Correct |
215 ms |
517556 KB |
Output is correct |
56 |
Correct |
325 ms |
757332 KB |
Output is correct |
57 |
Correct |
310 ms |
757456 KB |
Output is correct |
58 |
Correct |
324 ms |
757328 KB |
Output is correct |
59 |
Correct |
321 ms |
757332 KB |
Output is correct |
60 |
Correct |
305 ms |
751444 KB |
Output is correct |
61 |
Correct |
328 ms |
755280 KB |
Output is correct |
62 |
Correct |
323 ms |
757332 KB |
Output is correct |
63 |
Correct |
325 ms |
757456 KB |
Output is correct |
64 |
Correct |
330 ms |
757368 KB |
Output is correct |
65 |
Correct |
318 ms |
755396 KB |
Output is correct |
66 |
Correct |
327 ms |
747860 KB |
Output is correct |
67 |
Correct |
323 ms |
757332 KB |
Output is correct |
68 |
Correct |
336 ms |
755376 KB |
Output is correct |
69 |
Correct |
325 ms |
757508 KB |
Output is correct |
70 |
Correct |
340 ms |
757332 KB |
Output is correct |
71 |
Correct |
329 ms |
755272 KB |
Output is correct |
72 |
Correct |
329 ms |
755284 KB |
Output is correct |
73 |
Correct |
321 ms |
757328 KB |
Output is correct |