#include "bits/stdc++.h"
using namespace std;
const int maxn = 405;
int dp[maxn][maxn][maxn][3];
vector<int> inp[3];
int prv[3][maxn], a[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for(int i = 0; i < n; ++i){
if(s[i] == 'R')a[i] = 0;
else if(s[i] == 'G')a[i] = 1;
else a[i] = 2;
}
for(int i = 0; i < n; ++i){
inp[a[i]].push_back(i);
for(int j = 0; j < 3; ++j){
prv[j][i] = inp[j].size();
}
}
memset(dp, 63, sizeof dp);
for(int i = 0; i < 3; ++i){
dp[0][0][0][i] = 0;
}
for(int a = 0; a <= (int)inp[0].size(); ++a){
for(int b = 0; b <= (int)inp[1].size(); ++b){
for(int c = 0; c <= (int)inp[2].size(); ++c){
for(int last = 0; last < 3; ++last){
if(last != 0 && a + 1 <= inp[0].size()){
int pos = inp[0][a];
int mn = dp[a][b][c][last] + max(0, prv[1][pos] - b) + max(0, prv[2][pos] - c);
dp[a + 1][b][c][0] = min(dp[a + 1][b][c][0], mn);
}
if(last != 1 && b + 1 <= inp[1].size()){
int pos = inp[1][b];
int mn = dp[a][b][c][last] + max(0, prv[0][pos] - a) + max(0, prv[2][pos] - c);
dp[a][b + 1][c][1] = min(dp[a][b + 1][c][1], mn);
}
if(last != 2 && c + 1 <= inp[2].size()){
int pos = inp[2][c];
int mn = dp[a][b][c][last] + max(0, prv[0][pos] - a) + max(0, prv[1][pos] - b);
dp[a][b][c + 1][2] = min(dp[a][b][c + 1][2], mn);
}
}
}
}
}
int ans = 1e8;
for(int i = 0; i < 3; ++i){
ans = min(ans, dp[(int)inp[0].size()][(int)inp[1].size()][(int)inp[2].size()][i]);
}
if(ans == 1e8){
cout << "-1" << '\n';
}else {
cout << ans << '\n';
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main(int, const char**)':
joi2019_ho_t3.cpp:38:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(last != 0 && a + 1 <= inp[0].size()){
| ~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if(last != 1 && b + 1 <= inp[1].size()){
| ~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:48:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(last != 2 && c + 1 <= inp[2].size()){
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
780396 KB |
Output is correct |
2 |
Correct |
440 ms |
780396 KB |
Output is correct |
3 |
Correct |
415 ms |
780396 KB |
Output is correct |
4 |
Correct |
418 ms |
780396 KB |
Output is correct |
5 |
Correct |
421 ms |
780396 KB |
Output is correct |
6 |
Correct |
420 ms |
780404 KB |
Output is correct |
7 |
Correct |
423 ms |
780428 KB |
Output is correct |
8 |
Correct |
429 ms |
780396 KB |
Output is correct |
9 |
Correct |
422 ms |
780396 KB |
Output is correct |
10 |
Correct |
420 ms |
780524 KB |
Output is correct |
11 |
Correct |
415 ms |
780300 KB |
Output is correct |
12 |
Correct |
416 ms |
780396 KB |
Output is correct |
13 |
Correct |
416 ms |
780376 KB |
Output is correct |
14 |
Correct |
418 ms |
780524 KB |
Output is correct |
15 |
Correct |
416 ms |
780396 KB |
Output is correct |
16 |
Correct |
417 ms |
780396 KB |
Output is correct |
17 |
Correct |
424 ms |
780364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
780396 KB |
Output is correct |
2 |
Correct |
440 ms |
780396 KB |
Output is correct |
3 |
Correct |
415 ms |
780396 KB |
Output is correct |
4 |
Correct |
418 ms |
780396 KB |
Output is correct |
5 |
Correct |
421 ms |
780396 KB |
Output is correct |
6 |
Correct |
420 ms |
780404 KB |
Output is correct |
7 |
Correct |
423 ms |
780428 KB |
Output is correct |
8 |
Correct |
429 ms |
780396 KB |
Output is correct |
9 |
Correct |
422 ms |
780396 KB |
Output is correct |
10 |
Correct |
420 ms |
780524 KB |
Output is correct |
11 |
Correct |
415 ms |
780300 KB |
Output is correct |
12 |
Correct |
416 ms |
780396 KB |
Output is correct |
13 |
Correct |
416 ms |
780376 KB |
Output is correct |
14 |
Correct |
418 ms |
780524 KB |
Output is correct |
15 |
Correct |
416 ms |
780396 KB |
Output is correct |
16 |
Correct |
417 ms |
780396 KB |
Output is correct |
17 |
Correct |
424 ms |
780364 KB |
Output is correct |
18 |
Correct |
415 ms |
780396 KB |
Output is correct |
19 |
Correct |
435 ms |
780396 KB |
Output is correct |
20 |
Correct |
412 ms |
780396 KB |
Output is correct |
21 |
Correct |
434 ms |
780504 KB |
Output is correct |
22 |
Correct |
421 ms |
780524 KB |
Output is correct |
23 |
Correct |
417 ms |
780524 KB |
Output is correct |
24 |
Correct |
416 ms |
780396 KB |
Output is correct |
25 |
Correct |
420 ms |
780396 KB |
Output is correct |
26 |
Correct |
419 ms |
780396 KB |
Output is correct |
27 |
Correct |
418 ms |
780396 KB |
Output is correct |
28 |
Correct |
444 ms |
780396 KB |
Output is correct |
29 |
Correct |
419 ms |
780396 KB |
Output is correct |
30 |
Correct |
417 ms |
780396 KB |
Output is correct |
31 |
Correct |
430 ms |
780396 KB |
Output is correct |
32 |
Correct |
431 ms |
780524 KB |
Output is correct |
33 |
Correct |
421 ms |
780396 KB |
Output is correct |
34 |
Correct |
417 ms |
780396 KB |
Output is correct |
35 |
Correct |
418 ms |
780396 KB |
Output is correct |
36 |
Correct |
432 ms |
780396 KB |
Output is correct |
37 |
Correct |
424 ms |
780436 KB |
Output is correct |
38 |
Correct |
435 ms |
780396 KB |
Output is correct |
39 |
Correct |
420 ms |
780396 KB |
Output is correct |
40 |
Correct |
426 ms |
780396 KB |
Output is correct |
41 |
Correct |
423 ms |
780396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
780396 KB |
Output is correct |
2 |
Correct |
419 ms |
780396 KB |
Output is correct |
3 |
Correct |
423 ms |
780396 KB |
Output is correct |
4 |
Correct |
421 ms |
780396 KB |
Output is correct |
5 |
Correct |
423 ms |
780396 KB |
Output is correct |
6 |
Correct |
433 ms |
780524 KB |
Output is correct |
7 |
Correct |
419 ms |
780396 KB |
Output is correct |
8 |
Correct |
431 ms |
780524 KB |
Output is correct |
9 |
Correct |
435 ms |
780396 KB |
Output is correct |
10 |
Correct |
442 ms |
780332 KB |
Output is correct |
11 |
Correct |
428 ms |
780396 KB |
Output is correct |
12 |
Correct |
436 ms |
780396 KB |
Output is correct |
13 |
Correct |
433 ms |
780420 KB |
Output is correct |
14 |
Correct |
418 ms |
780396 KB |
Output is correct |
15 |
Correct |
420 ms |
780360 KB |
Output is correct |
16 |
Correct |
424 ms |
780396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
780396 KB |
Output is correct |
2 |
Correct |
440 ms |
780396 KB |
Output is correct |
3 |
Correct |
415 ms |
780396 KB |
Output is correct |
4 |
Correct |
418 ms |
780396 KB |
Output is correct |
5 |
Correct |
421 ms |
780396 KB |
Output is correct |
6 |
Correct |
420 ms |
780404 KB |
Output is correct |
7 |
Correct |
423 ms |
780428 KB |
Output is correct |
8 |
Correct |
429 ms |
780396 KB |
Output is correct |
9 |
Correct |
422 ms |
780396 KB |
Output is correct |
10 |
Correct |
420 ms |
780524 KB |
Output is correct |
11 |
Correct |
415 ms |
780300 KB |
Output is correct |
12 |
Correct |
416 ms |
780396 KB |
Output is correct |
13 |
Correct |
416 ms |
780376 KB |
Output is correct |
14 |
Correct |
418 ms |
780524 KB |
Output is correct |
15 |
Correct |
416 ms |
780396 KB |
Output is correct |
16 |
Correct |
417 ms |
780396 KB |
Output is correct |
17 |
Correct |
424 ms |
780364 KB |
Output is correct |
18 |
Correct |
415 ms |
780396 KB |
Output is correct |
19 |
Correct |
435 ms |
780396 KB |
Output is correct |
20 |
Correct |
412 ms |
780396 KB |
Output is correct |
21 |
Correct |
434 ms |
780504 KB |
Output is correct |
22 |
Correct |
421 ms |
780524 KB |
Output is correct |
23 |
Correct |
417 ms |
780524 KB |
Output is correct |
24 |
Correct |
416 ms |
780396 KB |
Output is correct |
25 |
Correct |
420 ms |
780396 KB |
Output is correct |
26 |
Correct |
419 ms |
780396 KB |
Output is correct |
27 |
Correct |
418 ms |
780396 KB |
Output is correct |
28 |
Correct |
444 ms |
780396 KB |
Output is correct |
29 |
Correct |
419 ms |
780396 KB |
Output is correct |
30 |
Correct |
417 ms |
780396 KB |
Output is correct |
31 |
Correct |
430 ms |
780396 KB |
Output is correct |
32 |
Correct |
431 ms |
780524 KB |
Output is correct |
33 |
Correct |
421 ms |
780396 KB |
Output is correct |
34 |
Correct |
417 ms |
780396 KB |
Output is correct |
35 |
Correct |
418 ms |
780396 KB |
Output is correct |
36 |
Correct |
432 ms |
780396 KB |
Output is correct |
37 |
Correct |
424 ms |
780436 KB |
Output is correct |
38 |
Correct |
435 ms |
780396 KB |
Output is correct |
39 |
Correct |
420 ms |
780396 KB |
Output is correct |
40 |
Correct |
426 ms |
780396 KB |
Output is correct |
41 |
Correct |
423 ms |
780396 KB |
Output is correct |
42 |
Correct |
425 ms |
780396 KB |
Output is correct |
43 |
Correct |
419 ms |
780396 KB |
Output is correct |
44 |
Correct |
423 ms |
780396 KB |
Output is correct |
45 |
Correct |
421 ms |
780396 KB |
Output is correct |
46 |
Correct |
423 ms |
780396 KB |
Output is correct |
47 |
Correct |
433 ms |
780524 KB |
Output is correct |
48 |
Correct |
419 ms |
780396 KB |
Output is correct |
49 |
Correct |
431 ms |
780524 KB |
Output is correct |
50 |
Correct |
435 ms |
780396 KB |
Output is correct |
51 |
Correct |
442 ms |
780332 KB |
Output is correct |
52 |
Correct |
428 ms |
780396 KB |
Output is correct |
53 |
Correct |
436 ms |
780396 KB |
Output is correct |
54 |
Correct |
433 ms |
780420 KB |
Output is correct |
55 |
Correct |
418 ms |
780396 KB |
Output is correct |
56 |
Correct |
420 ms |
780360 KB |
Output is correct |
57 |
Correct |
424 ms |
780396 KB |
Output is correct |
58 |
Correct |
464 ms |
780396 KB |
Output is correct |
59 |
Correct |
456 ms |
780396 KB |
Output is correct |
60 |
Correct |
452 ms |
780396 KB |
Output is correct |
61 |
Correct |
455 ms |
780396 KB |
Output is correct |
62 |
Correct |
426 ms |
780396 KB |
Output is correct |
63 |
Correct |
425 ms |
780396 KB |
Output is correct |
64 |
Correct |
439 ms |
780396 KB |
Output is correct |
65 |
Correct |
450 ms |
780396 KB |
Output is correct |
66 |
Correct |
458 ms |
780396 KB |
Output is correct |
67 |
Correct |
462 ms |
780396 KB |
Output is correct |
68 |
Correct |
454 ms |
780396 KB |
Output is correct |
69 |
Correct |
459 ms |
780524 KB |
Output is correct |
70 |
Correct |
460 ms |
780396 KB |
Output is correct |
71 |
Correct |
458 ms |
780524 KB |
Output is correct |
72 |
Correct |
429 ms |
780396 KB |
Output is correct |
73 |
Correct |
421 ms |
780396 KB |
Output is correct |