#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()) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1488 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 |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1488 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 |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
8 ms |
17944 KB |
Output is correct |
19 |
Correct |
8 ms |
18124 KB |
Output is correct |
20 |
Correct |
7 ms |
18012 KB |
Output is correct |
21 |
Correct |
8 ms |
18012 KB |
Output is correct |
22 |
Correct |
7 ms |
18008 KB |
Output is correct |
23 |
Correct |
7 ms |
18012 KB |
Output is correct |
24 |
Correct |
6 ms |
18012 KB |
Output is correct |
25 |
Correct |
6 ms |
18012 KB |
Output is correct |
26 |
Correct |
9 ms |
18012 KB |
Output is correct |
27 |
Correct |
8 ms |
17992 KB |
Output is correct |
28 |
Correct |
8 ms |
18012 KB |
Output is correct |
29 |
Correct |
7 ms |
17944 KB |
Output is correct |
30 |
Correct |
8 ms |
18008 KB |
Output is correct |
31 |
Correct |
7 ms |
18012 KB |
Output is correct |
32 |
Correct |
7 ms |
18012 KB |
Output is correct |
33 |
Correct |
8 ms |
17372 KB |
Output is correct |
34 |
Correct |
7 ms |
17500 KB |
Output is correct |
35 |
Correct |
7 ms |
16804 KB |
Output is correct |
36 |
Correct |
7 ms |
17360 KB |
Output is correct |
37 |
Correct |
8 ms |
16336 KB |
Output is correct |
38 |
Correct |
7 ms |
18012 KB |
Output is correct |
39 |
Correct |
9 ms |
17956 KB |
Output is correct |
40 |
Correct |
7 ms |
17496 KB |
Output is correct |
41 |
Correct |
6 ms |
18132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
364 ms |
757524 KB |
Output is correct |
3 |
Correct |
362 ms |
755284 KB |
Output is correct |
4 |
Correct |
353 ms |
757332 KB |
Output is correct |
5 |
Correct |
413 ms |
757324 KB |
Output is correct |
6 |
Correct |
401 ms |
757368 KB |
Output is correct |
7 |
Correct |
350 ms |
755240 KB |
Output is correct |
8 |
Correct |
328 ms |
755276 KB |
Output is correct |
9 |
Correct |
388 ms |
751480 KB |
Output is correct |
10 |
Correct |
349 ms |
757324 KB |
Output is correct |
11 |
Correct |
347 ms |
757340 KB |
Output is correct |
12 |
Correct |
80 ms |
202892 KB |
Output is correct |
13 |
Correct |
145 ms |
357396 KB |
Output is correct |
14 |
Correct |
229 ms |
517460 KB |
Output is correct |
15 |
Correct |
353 ms |
757328 KB |
Output is correct |
16 |
Correct |
334 ms |
757332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1488 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 |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
8 ms |
17944 KB |
Output is correct |
19 |
Correct |
8 ms |
18124 KB |
Output is correct |
20 |
Correct |
7 ms |
18012 KB |
Output is correct |
21 |
Correct |
8 ms |
18012 KB |
Output is correct |
22 |
Correct |
7 ms |
18008 KB |
Output is correct |
23 |
Correct |
7 ms |
18012 KB |
Output is correct |
24 |
Correct |
6 ms |
18012 KB |
Output is correct |
25 |
Correct |
6 ms |
18012 KB |
Output is correct |
26 |
Correct |
9 ms |
18012 KB |
Output is correct |
27 |
Correct |
8 ms |
17992 KB |
Output is correct |
28 |
Correct |
8 ms |
18012 KB |
Output is correct |
29 |
Correct |
7 ms |
17944 KB |
Output is correct |
30 |
Correct |
8 ms |
18008 KB |
Output is correct |
31 |
Correct |
7 ms |
18012 KB |
Output is correct |
32 |
Correct |
7 ms |
18012 KB |
Output is correct |
33 |
Correct |
8 ms |
17372 KB |
Output is correct |
34 |
Correct |
7 ms |
17500 KB |
Output is correct |
35 |
Correct |
7 ms |
16804 KB |
Output is correct |
36 |
Correct |
7 ms |
17360 KB |
Output is correct |
37 |
Correct |
8 ms |
16336 KB |
Output is correct |
38 |
Correct |
7 ms |
18012 KB |
Output is correct |
39 |
Correct |
9 ms |
17956 KB |
Output is correct |
40 |
Correct |
7 ms |
17496 KB |
Output is correct |
41 |
Correct |
6 ms |
18132 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
364 ms |
757524 KB |
Output is correct |
44 |
Correct |
362 ms |
755284 KB |
Output is correct |
45 |
Correct |
353 ms |
757332 KB |
Output is correct |
46 |
Correct |
413 ms |
757324 KB |
Output is correct |
47 |
Correct |
401 ms |
757368 KB |
Output is correct |
48 |
Correct |
350 ms |
755240 KB |
Output is correct |
49 |
Correct |
328 ms |
755276 KB |
Output is correct |
50 |
Correct |
388 ms |
751480 KB |
Output is correct |
51 |
Correct |
349 ms |
757324 KB |
Output is correct |
52 |
Correct |
347 ms |
757340 KB |
Output is correct |
53 |
Correct |
80 ms |
202892 KB |
Output is correct |
54 |
Correct |
145 ms |
357396 KB |
Output is correct |
55 |
Correct |
229 ms |
517460 KB |
Output is correct |
56 |
Correct |
353 ms |
757328 KB |
Output is correct |
57 |
Correct |
334 ms |
757332 KB |
Output is correct |
58 |
Correct |
382 ms |
757328 KB |
Output is correct |
59 |
Correct |
360 ms |
757328 KB |
Output is correct |
60 |
Correct |
352 ms |
751444 KB |
Output is correct |
61 |
Correct |
355 ms |
755284 KB |
Output is correct |
62 |
Correct |
348 ms |
757380 KB |
Output is correct |
63 |
Correct |
349 ms |
757332 KB |
Output is correct |
64 |
Correct |
362 ms |
757332 KB |
Output is correct |
65 |
Correct |
364 ms |
755180 KB |
Output is correct |
66 |
Correct |
338 ms |
747860 KB |
Output is correct |
67 |
Correct |
338 ms |
757332 KB |
Output is correct |
68 |
Correct |
351 ms |
755344 KB |
Output is correct |
69 |
Correct |
349 ms |
757332 KB |
Output is correct |
70 |
Correct |
368 ms |
757408 KB |
Output is correct |
71 |
Correct |
356 ms |
755288 KB |
Output is correct |
72 |
Correct |
344 ms |
755280 KB |
Output is correct |
73 |
Correct |
317 ms |
757336 KB |
Output is correct |