#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 200 + 10;
const int inf = 1e9;
int dp[maxn][maxn][maxn][3];
int Gglob, Rglob, Yglob;
vector<int> G, R, Y;
int cost(int from){ // age time shod O(1) esh mikonim
int nG = upper_bound(G.begin(), G.end(), from) - G.begin();
int nR = upper_bound(R.begin(), R.end(), from) - R.begin();
int nY = upper_bound(Y.begin(), Y.end(), from) - Y.begin();
return max(0, nG - Gglob) + max(0, nR - Rglob) + max(0, nY - Yglob);
}
int main(){
ios_base::sync_with_stdio(false);
int n;
string s;
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);
}
if ((int)G.size() < (int)R.size())
swap(G, R);
if ((int)G.size() < (int)Y.size())
swap(G, Y);
if ((int)R.size() < (int)Y.size())
swap(R, Y);
if ((int)G.size() * 2 - 1 > n)
return cout << -1 << endl, 0;
memset(dp, 63, sizeof dp);
for (int g = 0; g <= (int)G.size(); g++){
for (int r = 0; r <= (int)R.size(); r++){
for (int y = 0; y <= (int)Y.size(); y++){
if (g + r + y == 0){
dp[g][r][y][0] = dp[g][r][y][1] = dp[g][r][y][2] = 0;
Yglob ++;
continue;
}
if (2 * max({g, r, y}) - 1 > g + r + y){
Yglob ++;
continue;
}
int now = g + r + y - 1;
if (g > 0)
dp[g][r][y][0] = min(inf, min(dp[g-1][r][y][1], dp[g-1][r][y][2]) + cost(G[g - 1]));
if (r > 0)
dp[g][r][y][1] = min(inf, min(dp[g][r-1][y][0], dp[g][r-1][y][2]) + cost(R[r - 1]));
if (y > 0)
dp[g][r][y][2] = min(inf, min(dp[g][r][y-1][0], dp[g][r][y-1][1]) + cost(Y[y - 1]));
Yglob ++;
}
Rglob ++;
Yglob = 0;
}
Gglob ++;
Rglob = 0;
}
int g = (int)G.size(), r = (int)R.size(), y = (int)Y.size();
int answer = min({dp[g][r][y][0], dp[g][r][y][1], dp[g][r][y][2]});
cout << answer << '\n';
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:57:9: warning: unused variable 'now' [-Wunused-variable]
int now = g + r + y - 1;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
109048 KB |
Output is correct |
2 |
Correct |
63 ms |
109048 KB |
Output is correct |
3 |
Correct |
61 ms |
109048 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
62 ms |
109048 KB |
Output is correct |
6 |
Correct |
61 ms |
109068 KB |
Output is correct |
7 |
Correct |
62 ms |
109048 KB |
Output is correct |
8 |
Correct |
62 ms |
109048 KB |
Output is correct |
9 |
Correct |
62 ms |
109048 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
62 ms |
109048 KB |
Output is correct |
12 |
Correct |
62 ms |
109048 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
60 ms |
109052 KB |
Output is correct |
15 |
Correct |
62 ms |
109048 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
109048 KB |
Output is correct |
2 |
Correct |
63 ms |
109048 KB |
Output is correct |
3 |
Correct |
61 ms |
109048 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
62 ms |
109048 KB |
Output is correct |
6 |
Correct |
61 ms |
109068 KB |
Output is correct |
7 |
Correct |
62 ms |
109048 KB |
Output is correct |
8 |
Correct |
62 ms |
109048 KB |
Output is correct |
9 |
Correct |
62 ms |
109048 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
62 ms |
109048 KB |
Output is correct |
12 |
Correct |
62 ms |
109048 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
60 ms |
109052 KB |
Output is correct |
15 |
Correct |
62 ms |
109048 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
376 KB |
Output is correct |
18 |
Correct |
60 ms |
109048 KB |
Output is correct |
19 |
Correct |
61 ms |
109052 KB |
Output is correct |
20 |
Correct |
61 ms |
109112 KB |
Output is correct |
21 |
Correct |
62 ms |
109048 KB |
Output is correct |
22 |
Correct |
64 ms |
109308 KB |
Output is correct |
23 |
Correct |
61 ms |
109048 KB |
Output is correct |
24 |
Correct |
62 ms |
109048 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
408 KB |
Output is correct |
27 |
Correct |
62 ms |
109048 KB |
Output is correct |
28 |
Correct |
62 ms |
109048 KB |
Output is correct |
29 |
Correct |
61 ms |
109048 KB |
Output is correct |
30 |
Correct |
60 ms |
109048 KB |
Output is correct |
31 |
Correct |
61 ms |
109048 KB |
Output is correct |
32 |
Correct |
64 ms |
109048 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
62 ms |
109048 KB |
Output is correct |
36 |
Correct |
63 ms |
109048 KB |
Output is correct |
37 |
Correct |
64 ms |
109048 KB |
Output is correct |
38 |
Correct |
63 ms |
109048 KB |
Output is correct |
39 |
Correct |
60 ms |
109048 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
109048 KB |
Output is correct |
2 |
Correct |
66 ms |
109048 KB |
Output is correct |
3 |
Correct |
60 ms |
109048 KB |
Output is correct |
4 |
Correct |
61 ms |
109048 KB |
Output is correct |
5 |
Correct |
60 ms |
109048 KB |
Output is correct |
6 |
Correct |
61 ms |
109048 KB |
Output is correct |
7 |
Correct |
61 ms |
109048 KB |
Output is correct |
8 |
Correct |
62 ms |
109048 KB |
Output is correct |
9 |
Correct |
63 ms |
109048 KB |
Output is correct |
10 |
Correct |
63 ms |
109176 KB |
Output is correct |
11 |
Correct |
61 ms |
109092 KB |
Output is correct |
12 |
Correct |
62 ms |
109048 KB |
Output is correct |
13 |
Correct |
62 ms |
109048 KB |
Output is correct |
14 |
Correct |
60 ms |
109048 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
109048 KB |
Output is correct |
2 |
Correct |
63 ms |
109048 KB |
Output is correct |
3 |
Correct |
61 ms |
109048 KB |
Output is correct |
4 |
Correct |
63 ms |
109048 KB |
Output is correct |
5 |
Correct |
62 ms |
109048 KB |
Output is correct |
6 |
Correct |
61 ms |
109068 KB |
Output is correct |
7 |
Correct |
62 ms |
109048 KB |
Output is correct |
8 |
Correct |
62 ms |
109048 KB |
Output is correct |
9 |
Correct |
62 ms |
109048 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
62 ms |
109048 KB |
Output is correct |
12 |
Correct |
62 ms |
109048 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
60 ms |
109052 KB |
Output is correct |
15 |
Correct |
62 ms |
109048 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
376 KB |
Output is correct |
18 |
Correct |
60 ms |
109048 KB |
Output is correct |
19 |
Correct |
61 ms |
109052 KB |
Output is correct |
20 |
Correct |
61 ms |
109112 KB |
Output is correct |
21 |
Correct |
62 ms |
109048 KB |
Output is correct |
22 |
Correct |
64 ms |
109308 KB |
Output is correct |
23 |
Correct |
61 ms |
109048 KB |
Output is correct |
24 |
Correct |
62 ms |
109048 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
408 KB |
Output is correct |
27 |
Correct |
62 ms |
109048 KB |
Output is correct |
28 |
Correct |
62 ms |
109048 KB |
Output is correct |
29 |
Correct |
61 ms |
109048 KB |
Output is correct |
30 |
Correct |
60 ms |
109048 KB |
Output is correct |
31 |
Correct |
61 ms |
109048 KB |
Output is correct |
32 |
Correct |
64 ms |
109048 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
62 ms |
109048 KB |
Output is correct |
36 |
Correct |
63 ms |
109048 KB |
Output is correct |
37 |
Correct |
64 ms |
109048 KB |
Output is correct |
38 |
Correct |
63 ms |
109048 KB |
Output is correct |
39 |
Correct |
60 ms |
109048 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
42 |
Correct |
64 ms |
109048 KB |
Output is correct |
43 |
Correct |
66 ms |
109048 KB |
Output is correct |
44 |
Correct |
60 ms |
109048 KB |
Output is correct |
45 |
Correct |
61 ms |
109048 KB |
Output is correct |
46 |
Correct |
60 ms |
109048 KB |
Output is correct |
47 |
Correct |
61 ms |
109048 KB |
Output is correct |
48 |
Correct |
61 ms |
109048 KB |
Output is correct |
49 |
Correct |
62 ms |
109048 KB |
Output is correct |
50 |
Correct |
63 ms |
109048 KB |
Output is correct |
51 |
Correct |
63 ms |
109176 KB |
Output is correct |
52 |
Correct |
61 ms |
109092 KB |
Output is correct |
53 |
Correct |
62 ms |
109048 KB |
Output is correct |
54 |
Correct |
62 ms |
109048 KB |
Output is correct |
55 |
Correct |
60 ms |
109048 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
244 ms |
109048 KB |
Output is correct |
59 |
Correct |
253 ms |
109048 KB |
Output is correct |
60 |
Correct |
247 ms |
109048 KB |
Output is correct |
61 |
Correct |
253 ms |
109176 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
65 ms |
109048 KB |
Output is correct |
64 |
Correct |
96 ms |
109176 KB |
Output is correct |
65 |
Correct |
137 ms |
109048 KB |
Output is correct |
66 |
Correct |
218 ms |
109048 KB |
Output is correct |
67 |
Correct |
218 ms |
109048 KB |
Output is correct |
68 |
Correct |
228 ms |
109176 KB |
Output is correct |
69 |
Correct |
230 ms |
109048 KB |
Output is correct |
70 |
Correct |
231 ms |
109048 KB |
Output is correct |
71 |
Correct |
226 ms |
109176 KB |
Output is correct |
72 |
Correct |
5 ms |
376 KB |
Output is correct |
73 |
Correct |
5 ms |
380 KB |
Output is correct |