#include <bits/stdc++.h>
using namespace std;
vector<int> G , R , Y;
int n;
string s;
vector<vector<vector<int>>> dp[4];
int brute(int i , int j , int k , int lst){
int sz = i + j + k ;
if (sz == n) return 0;
if (~dp[lst][i][j][k]) return dp[lst][i][j][k];
int a = 1e8 , b = 1e8 , c = 1e8;
if (lst != 1 && i < G.size()){
int x = upper_bound(R.begin() , R.begin() + j , G[i]) - R.begin();
int y = upper_bound(Y.begin() , Y.begin() + k , G[i]) - Y.begin();
x = j-x; y = k-y;
a = brute(i+1 , j , k , 1) + x + y;
}
if (lst != 2 && j < R.size()){
int x = upper_bound(G.begin() , G.begin() + i , R[j]) - G.begin();
int y = upper_bound(Y.begin() , Y.begin() + k , R[j]) - Y.begin();
x = i-x; y = k-y;
b = brute(i , j+1 , k , 2) + x + y;
}
if (lst != 3 && k < Y.size()){
int x = upper_bound(G.begin() , G.begin() + i , Y[k]) - G.begin();
int y = upper_bound(R.begin() , R.begin() + j , Y[k]) - R.begin();
x = i-x; y = j-y;
c = brute(i , j , k+1 , 3) + x + y;
}
return dp[lst][i][j][k]= min({a , b , c});
}
void doWork(){
cin >> n >> s;
for (int i = 0 ; i < n ; i++){
if (s[i] == 'G') G.push_back(i);
else if (s[i] == 'R') R.push_back(i);
else Y.push_back(i);
}
for (int i = 0 ; i < 4 ; i++){
dp[i].resize(G.size() + 1);
for (auto &j : dp[i])
{ j.resize(R.size()+1); for (auto &k : j) k.resize(Y.size()+1 , -1); }
}
int ans = brute(0 , 0 , 0 , 0);
cout << (ans == 1e8 ? -1 : ans) ;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
doWork();
}
Compilation message
joi2019_ho_t3.cpp: In function 'int brute(int, int, int, int)':
joi2019_ho_t3.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if (lst != 1 && i < G.size()){
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if (lst != 2 && j < R.size()){
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if (lst != 3 && k < Y.size()){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
668 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
9 ms |
9304 KB |
Output is correct |
3 |
Correct |
8 ms |
9308 KB |
Output is correct |
4 |
Correct |
8 ms |
9308 KB |
Output is correct |
5 |
Correct |
12 ms |
9560 KB |
Output is correct |
6 |
Correct |
8 ms |
9308 KB |
Output is correct |
7 |
Correct |
8 ms |
9308 KB |
Output is correct |
8 |
Correct |
8 ms |
9308 KB |
Output is correct |
9 |
Correct |
8 ms |
9052 KB |
Output is correct |
10 |
Correct |
9 ms |
9148 KB |
Output is correct |
11 |
Correct |
8 ms |
9304 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
4444 KB |
Output is correct |
14 |
Correct |
7 ms |
6560 KB |
Output is correct |
15 |
Correct |
8 ms |
9308 KB |
Output is correct |
16 |
Correct |
11 ms |
9212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
668 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
544 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
9 ms |
9304 KB |
Output is correct |
44 |
Correct |
8 ms |
9308 KB |
Output is correct |
45 |
Correct |
8 ms |
9308 KB |
Output is correct |
46 |
Correct |
12 ms |
9560 KB |
Output is correct |
47 |
Correct |
8 ms |
9308 KB |
Output is correct |
48 |
Correct |
8 ms |
9308 KB |
Output is correct |
49 |
Correct |
8 ms |
9308 KB |
Output is correct |
50 |
Correct |
8 ms |
9052 KB |
Output is correct |
51 |
Correct |
9 ms |
9148 KB |
Output is correct |
52 |
Correct |
8 ms |
9304 KB |
Output is correct |
53 |
Correct |
3 ms |
2652 KB |
Output is correct |
54 |
Correct |
4 ms |
4444 KB |
Output is correct |
55 |
Correct |
7 ms |
6560 KB |
Output is correct |
56 |
Correct |
8 ms |
9308 KB |
Output is correct |
57 |
Correct |
11 ms |
9212 KB |
Output is correct |
58 |
Correct |
351 ms |
40892 KB |
Output is correct |
59 |
Correct |
349 ms |
41308 KB |
Output is correct |
60 |
Correct |
346 ms |
40040 KB |
Output is correct |
61 |
Correct |
358 ms |
40944 KB |
Output is correct |
62 |
Correct |
10 ms |
9052 KB |
Output is correct |
63 |
Correct |
15 ms |
11356 KB |
Output is correct |
64 |
Correct |
55 ms |
24920 KB |
Output is correct |
65 |
Correct |
130 ms |
30948 KB |
Output is correct |
66 |
Correct |
202 ms |
40400 KB |
Output is correct |
67 |
Correct |
186 ms |
40028 KB |
Output is correct |
68 |
Correct |
211 ms |
40452 KB |
Output is correct |
69 |
Correct |
256 ms |
41208 KB |
Output is correct |
70 |
Correct |
259 ms |
41024 KB |
Output is correct |
71 |
Correct |
288 ms |
40284 KB |
Output is correct |
72 |
Correct |
16 ms |
11100 KB |
Output is correct |
73 |
Correct |
2 ms |
2908 KB |
Output is correct |