#include <bits/stdc++.h>
#define DIM 402
#define INF 2000000000
using namespace std;
int dp[DIM][DIM][DIM][3],cnt[3][DIM];
vector <int> poz[3];
char v[DIM];
int n,i,j,k;
int main (){
// ifstream cin ("date.in");
// ofstream cout ("date.out");
cin>>n>>v+1;
for (i=1;i<=n;i++){
cnt[0][i] = cnt[0][i-1];
cnt[1][i] = cnt[1][i-1];
cnt[2][i] = cnt[2][i-1];
if (v[i] == 'R'){
poz[0].push_back(i);
cnt[0][i]++;
} else {
if (v[i] == 'G'){
poz[1].push_back(i);
cnt[1][i]++;
} else {
poz[2].push_back(i);
cnt[2][i]++;
}
}
}
/// dp[nr0][nr1][nr2][0/1/2]
for (i=0;i<=poz[0].size();i++)
for (j=0;j<=poz[1].size();j++)
for (k=0;k<=poz[2].size();k++)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = INF;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
int a = poz[0].size(), b = poz[1].size(), c = poz[2].size();
for (i=1;i<=n;i++)
for (int nr0=0;nr0<=min(i,a);nr0++)
for (int nr1=0;nr1<=min(i,b);nr1++)
for (int nr2=0;nr2<=min(i,c);nr2++){
if (nr0 + nr1 + nr2 > i)
continue;
/// incerc sa pun un 0
if (nr0){
int pos = poz[0][nr0-1];
int val = max (cnt[1][pos] - nr1,0) + max (cnt[2][pos] - nr2,0);
if (dp[nr0-1][nr1][nr2][1] != INF)
dp[nr0][nr1][nr2][0] = min (dp[nr0][nr1][nr2][0], dp[nr0-1][nr1][nr2][1] + val);
if (dp[nr0-1][nr1][nr2][2] != INF)
dp[nr0][nr1][nr2][0] = min (dp[nr0][nr1][nr2][0], dp[nr0-1][nr1][nr2][2] + val);
}
/// incerc sa pun 1
if (nr1){
int pos = poz[1][nr1-1];
int val = max (cnt[0][pos] - nr0,0) + max (cnt[2][pos] - nr2, 0);
if (dp[nr0][nr1-1][nr2][0] != INF)
dp[nr0][nr1][nr2][1] = min (dp[nr0][nr1][nr2][1], dp[nr0][nr1-1][nr2][0] + val);
if (dp[nr0][nr1-1][nr2][2] != INF)
dp[nr0][nr1][nr2][1] = min (dp[nr0][nr1][nr2][1], dp[nr0][nr1-1][nr2][2] + val);
}
/// incerec sa pun 2
if (nr2){
int pos = poz[2][nr2-1];
int val = max (cnt[0][pos] - nr0,0) + max (cnt[1][pos] - nr1, 0);
if (dp[nr0][nr1][nr2-1][0] != INF)
dp[nr0][nr1][nr2][2] = min (dp[nr0][nr1][nr2][2], dp[nr0][nr1][nr2-1][0] + val);
if (dp[nr0][nr1][nr2-1][1] != INF)
dp[nr0][nr1][nr2][2] = min (dp[nr0][nr1][nr2][2], dp[nr0][nr1][nr2-1][1] + val);
}
}
int sol = INF;
sol = min (sol, dp[a][b][c][0]);
sol = min (sol, dp[a][b][c][1]);
sol = min (sol, dp[a][b][c][2]);
if (sol == INF)
cout<<-1;
else cout<<sol;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:14:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin>>n>>v+1;
~^~
joi2019_ho_t3.cpp:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<=poz[0].size();i++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:37:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j=0;j<=poz[1].size();j++)
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (k=0;k<=poz[2].size();k++)
~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
8 ms |
2304 KB |
Output is correct |
19 |
Correct |
8 ms |
1920 KB |
Output is correct |
20 |
Correct |
8 ms |
2304 KB |
Output is correct |
21 |
Correct |
8 ms |
2432 KB |
Output is correct |
22 |
Correct |
7 ms |
1664 KB |
Output is correct |
23 |
Correct |
11 ms |
2304 KB |
Output is correct |
24 |
Correct |
8 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
7 ms |
4224 KB |
Output is correct |
27 |
Correct |
8 ms |
3072 KB |
Output is correct |
28 |
Correct |
12 ms |
2304 KB |
Output is correct |
29 |
Correct |
12 ms |
2304 KB |
Output is correct |
30 |
Correct |
8 ms |
2304 KB |
Output is correct |
31 |
Correct |
8 ms |
2048 KB |
Output is correct |
32 |
Correct |
9 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
3840 KB |
Output is correct |
34 |
Correct |
7 ms |
3584 KB |
Output is correct |
35 |
Correct |
8 ms |
2688 KB |
Output is correct |
36 |
Correct |
8 ms |
1920 KB |
Output is correct |
37 |
Correct |
7 ms |
1792 KB |
Output is correct |
38 |
Correct |
8 ms |
2176 KB |
Output is correct |
39 |
Correct |
8 ms |
2304 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
6 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
257 ms |
163064 KB |
Output is correct |
3 |
Correct |
240 ms |
162296 KB |
Output is correct |
4 |
Correct |
229 ms |
163068 KB |
Output is correct |
5 |
Correct |
241 ms |
162936 KB |
Output is correct |
6 |
Correct |
251 ms |
162936 KB |
Output is correct |
7 |
Correct |
249 ms |
162168 KB |
Output is correct |
8 |
Correct |
235 ms |
162168 KB |
Output is correct |
9 |
Correct |
268 ms |
161400 KB |
Output is correct |
10 |
Correct |
245 ms |
163064 KB |
Output is correct |
11 |
Correct |
229 ms |
162936 KB |
Output is correct |
12 |
Correct |
41 ms |
44032 KB |
Output is correct |
13 |
Correct |
80 ms |
77304 KB |
Output is correct |
14 |
Correct |
132 ms |
111352 KB |
Output is correct |
15 |
Correct |
233 ms |
163192 KB |
Output is correct |
16 |
Correct |
247 ms |
163192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
8 ms |
2304 KB |
Output is correct |
19 |
Correct |
8 ms |
1920 KB |
Output is correct |
20 |
Correct |
8 ms |
2304 KB |
Output is correct |
21 |
Correct |
8 ms |
2432 KB |
Output is correct |
22 |
Correct |
7 ms |
1664 KB |
Output is correct |
23 |
Correct |
11 ms |
2304 KB |
Output is correct |
24 |
Correct |
8 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
7 ms |
4224 KB |
Output is correct |
27 |
Correct |
8 ms |
3072 KB |
Output is correct |
28 |
Correct |
12 ms |
2304 KB |
Output is correct |
29 |
Correct |
12 ms |
2304 KB |
Output is correct |
30 |
Correct |
8 ms |
2304 KB |
Output is correct |
31 |
Correct |
8 ms |
2048 KB |
Output is correct |
32 |
Correct |
9 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
3840 KB |
Output is correct |
34 |
Correct |
7 ms |
3584 KB |
Output is correct |
35 |
Correct |
8 ms |
2688 KB |
Output is correct |
36 |
Correct |
8 ms |
1920 KB |
Output is correct |
37 |
Correct |
7 ms |
1792 KB |
Output is correct |
38 |
Correct |
8 ms |
2176 KB |
Output is correct |
39 |
Correct |
8 ms |
2304 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
6 ms |
2176 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
257 ms |
163064 KB |
Output is correct |
44 |
Correct |
240 ms |
162296 KB |
Output is correct |
45 |
Correct |
229 ms |
163068 KB |
Output is correct |
46 |
Correct |
241 ms |
162936 KB |
Output is correct |
47 |
Correct |
251 ms |
162936 KB |
Output is correct |
48 |
Correct |
249 ms |
162168 KB |
Output is correct |
49 |
Correct |
235 ms |
162168 KB |
Output is correct |
50 |
Correct |
268 ms |
161400 KB |
Output is correct |
51 |
Correct |
245 ms |
163064 KB |
Output is correct |
52 |
Correct |
229 ms |
162936 KB |
Output is correct |
53 |
Correct |
41 ms |
44032 KB |
Output is correct |
54 |
Correct |
80 ms |
77304 KB |
Output is correct |
55 |
Correct |
132 ms |
111352 KB |
Output is correct |
56 |
Correct |
233 ms |
163192 KB |
Output is correct |
57 |
Correct |
247 ms |
163192 KB |
Output is correct |
58 |
Execution timed out |
1092 ms |
75640 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |