#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int inf = 1e9+5;
int dp[400][401][401][3];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; str ss; cin >> n >> ss;
vector<int> s;
for(int i = 0; i < n; i++){
if(ss[i] == 'R') s.pb(0);
if(ss[i] == 'G') s.pb(1);
if(ss[i] == 'Y') s.pb(2);
}
vector<vector<int>> cnt(n, vector<int>(3, 0));
vector<vector<int>> pos(3);
for(int i = 0; i < n; i++) for(int j = 0; j <= n; j++) for(int k = 0; k <= n; k++)
for(int c = 0; c < 3; c++) dp[i][j][k][c] = inf;
for(int i = n-1; i >= 0; i--){
pos[s[i]].pb(i);
if(i != n-1) cnt[i][0] = cnt[i+1][0], cnt[i][1] = cnt[i+1][1], cnt[i][2] = cnt[i+1][2];
cnt[i][s[i]]++;
}
vector<vector<int>> o(3);
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if(i != j) o[i].pb(j);
for(int i = n-1; i >= 0; i--){
int c[3];
for(c[0] = 0; c[0] <= min(cnt[0][0], n-i); c[0]++) for(c[1] = 0; c[1] <= cnt[0][1] && c[1]+c[0] <= n-i; c[1]++){
c[2] = n-i-c[0]-c[1];
if(c[2] > cnt[0][2]) continue;
for(int cl = 0; cl < 3; cl++){
if(c[cl] == 0) continue;
if(c[cl] > pos[cl].size()) continue;
for(int j = 0; j < 2; j++){
int cur = 0;
c[cl]--;
if(i != n-1) cur+=dp[i+1][c[0]][c[1]][o[cl][j]];
c[cl]++;
if(i == n-1) cur+=abs(pos[cl][c[cl]-1]-i);
else cur+=abs(pos[cl][c[cl]-1]-max(0, c[o[cl][0]]-cnt[pos[cl][c[cl]-1]][o[cl][0]])
-max(0, c[o[cl][1]]-cnt[pos[cl][c[cl]-1]][o[cl][1]])-i);
dp[i][c[0]][c[1]][cl] = min(dp[i][c[0]][c[1]][cl], cur);
}
}
}
}
int ans = inf;
for(int i = 0; i <= n; i++) for(int j = 0; j <= n; j++) for(int c = 0; c < 3; c++)
ans = min(ans, dp[0][i][j][c]);
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if(c[cl] > pos[cl].size()) continue;
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1356 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1308 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1356 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1308 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
7 ms |
17612 KB |
Output is correct |
19 |
Correct |
8 ms |
17612 KB |
Output is correct |
20 |
Correct |
9 ms |
17676 KB |
Output is correct |
21 |
Correct |
8 ms |
17644 KB |
Output is correct |
22 |
Correct |
7 ms |
17740 KB |
Output is correct |
23 |
Correct |
8 ms |
17740 KB |
Output is correct |
24 |
Correct |
8 ms |
17612 KB |
Output is correct |
25 |
Correct |
8 ms |
17612 KB |
Output is correct |
26 |
Correct |
7 ms |
17616 KB |
Output is correct |
27 |
Correct |
8 ms |
17640 KB |
Output is correct |
28 |
Correct |
8 ms |
17652 KB |
Output is correct |
29 |
Correct |
8 ms |
17612 KB |
Output is correct |
30 |
Correct |
8 ms |
17612 KB |
Output is correct |
31 |
Correct |
10 ms |
17740 KB |
Output is correct |
32 |
Correct |
10 ms |
17612 KB |
Output is correct |
33 |
Correct |
7 ms |
17100 KB |
Output is correct |
34 |
Correct |
7 ms |
17112 KB |
Output is correct |
35 |
Correct |
7 ms |
16468 KB |
Output is correct |
36 |
Correct |
7 ms |
17108 KB |
Output is correct |
37 |
Correct |
7 ms |
15956 KB |
Output is correct |
38 |
Correct |
7 ms |
17632 KB |
Output is correct |
39 |
Correct |
7 ms |
17736 KB |
Output is correct |
40 |
Correct |
7 ms |
17100 KB |
Output is correct |
41 |
Correct |
7 ms |
17636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
328 ms |
755544 KB |
Output is correct |
3 |
Correct |
358 ms |
753392 KB |
Output is correct |
4 |
Correct |
352 ms |
755524 KB |
Output is correct |
5 |
Correct |
364 ms |
755488 KB |
Output is correct |
6 |
Correct |
331 ms |
755492 KB |
Output is correct |
7 |
Correct |
354 ms |
753336 KB |
Output is correct |
8 |
Correct |
368 ms |
753368 KB |
Output is correct |
9 |
Correct |
337 ms |
749508 KB |
Output is correct |
10 |
Correct |
338 ms |
755528 KB |
Output is correct |
11 |
Correct |
330 ms |
755448 KB |
Output is correct |
12 |
Correct |
83 ms |
201708 KB |
Output is correct |
13 |
Correct |
152 ms |
356096 KB |
Output is correct |
14 |
Correct |
216 ms |
515796 KB |
Output is correct |
15 |
Correct |
347 ms |
755540 KB |
Output is correct |
16 |
Correct |
328 ms |
755536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1356 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1308 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
7 ms |
17612 KB |
Output is correct |
19 |
Correct |
8 ms |
17612 KB |
Output is correct |
20 |
Correct |
9 ms |
17676 KB |
Output is correct |
21 |
Correct |
8 ms |
17644 KB |
Output is correct |
22 |
Correct |
7 ms |
17740 KB |
Output is correct |
23 |
Correct |
8 ms |
17740 KB |
Output is correct |
24 |
Correct |
8 ms |
17612 KB |
Output is correct |
25 |
Correct |
8 ms |
17612 KB |
Output is correct |
26 |
Correct |
7 ms |
17616 KB |
Output is correct |
27 |
Correct |
8 ms |
17640 KB |
Output is correct |
28 |
Correct |
8 ms |
17652 KB |
Output is correct |
29 |
Correct |
8 ms |
17612 KB |
Output is correct |
30 |
Correct |
8 ms |
17612 KB |
Output is correct |
31 |
Correct |
10 ms |
17740 KB |
Output is correct |
32 |
Correct |
10 ms |
17612 KB |
Output is correct |
33 |
Correct |
7 ms |
17100 KB |
Output is correct |
34 |
Correct |
7 ms |
17112 KB |
Output is correct |
35 |
Correct |
7 ms |
16468 KB |
Output is correct |
36 |
Correct |
7 ms |
17108 KB |
Output is correct |
37 |
Correct |
7 ms |
15956 KB |
Output is correct |
38 |
Correct |
7 ms |
17632 KB |
Output is correct |
39 |
Correct |
7 ms |
17736 KB |
Output is correct |
40 |
Correct |
7 ms |
17100 KB |
Output is correct |
41 |
Correct |
7 ms |
17636 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
328 ms |
755544 KB |
Output is correct |
44 |
Correct |
358 ms |
753392 KB |
Output is correct |
45 |
Correct |
352 ms |
755524 KB |
Output is correct |
46 |
Correct |
364 ms |
755488 KB |
Output is correct |
47 |
Correct |
331 ms |
755492 KB |
Output is correct |
48 |
Correct |
354 ms |
753336 KB |
Output is correct |
49 |
Correct |
368 ms |
753368 KB |
Output is correct |
50 |
Correct |
337 ms |
749508 KB |
Output is correct |
51 |
Correct |
338 ms |
755528 KB |
Output is correct |
52 |
Correct |
330 ms |
755448 KB |
Output is correct |
53 |
Correct |
83 ms |
201708 KB |
Output is correct |
54 |
Correct |
152 ms |
356096 KB |
Output is correct |
55 |
Correct |
216 ms |
515796 KB |
Output is correct |
56 |
Correct |
347 ms |
755540 KB |
Output is correct |
57 |
Correct |
328 ms |
755536 KB |
Output is correct |
58 |
Correct |
443 ms |
755528 KB |
Output is correct |
59 |
Correct |
453 ms |
755512 KB |
Output is correct |
60 |
Correct |
454 ms |
749668 KB |
Output is correct |
61 |
Correct |
449 ms |
753304 KB |
Output is correct |
62 |
Correct |
348 ms |
755604 KB |
Output is correct |
63 |
Correct |
359 ms |
755456 KB |
Output is correct |
64 |
Correct |
414 ms |
755548 KB |
Output is correct |
65 |
Correct |
413 ms |
753376 KB |
Output is correct |
66 |
Correct |
485 ms |
745988 KB |
Output is correct |
67 |
Correct |
438 ms |
755544 KB |
Output is correct |
68 |
Correct |
450 ms |
753476 KB |
Output is correct |
69 |
Correct |
463 ms |
755548 KB |
Output is correct |
70 |
Correct |
452 ms |
755552 KB |
Output is correct |
71 |
Correct |
456 ms |
753280 KB |
Output is correct |
72 |
Correct |
358 ms |
753304 KB |
Output is correct |
73 |
Correct |
330 ms |
755520 KB |
Output is correct |