#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
string s;
int n;
#define MAX 402
int conv(char x){
if (x == 'R') return 0;
if (x == 'Y') return 1;
if (x == 'G') return 2;
}
int cnt[3];
int dp[3][MAX][MAX][MAX];
int pref[3][MAX], pos[3][MAX];
void process(void){
cin >> n >> s;
for (int i = 0; i < n; ++i){
int c = conv(s[i]);
pos[c][cnt[c]++] = i;
pref[c][i]++;
REP(j, 3){
pref[j][i + 1] += pref[j][i];
}
}
memset(dp, 0x3f, sizeof dp);
REP(c, 3) REP(i, cnt[0] + 1) REP(j, cnt[1] + 1) REP(k, cnt[2] + 1) dp[c][i][j][k] = INF;
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
REP(i, cnt[0] + 1) REP(j, cnt[1] + 1) REP(k, cnt[2] + 1){
REP(last, 3) {
if (last != 0 && i < cnt[0]){
int loc = pos[0][i];
minimize(dp[0][i + 1][j][k], dp[last][i][j][k] + max(0, pref[1][loc] - j) + max(0, pref[2][loc] - k));
}
if (last != 1 && j < cnt[1]){
int loc = pos[1][j];
minimize(dp[1][i][j + 1][k], dp[last][i][j][k] + max(0, pref[0][loc] - i) + max(0, pref[2][loc] - k));
}
if (last != 2 && k < cnt[2]){
int loc = pos[2][k];
minimize(dp[2][i][j][k + 1], dp[last][i][j][k] + max(0, pref[0][loc] - i) + max(0, pref[1][loc] - j));
}
}
}
int ans = min({dp[0][cnt[0]][cnt[1]][cnt[2]], dp[1][cnt[0]][cnt[1]][cnt[2]], dp[2][cnt[0]][cnt[1]][cnt[2]]});
cout << (ans >= INF ? -1 : ans);
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
}
Compilation message
joi2019_ho_t3.cpp: In function 'int conv(char)':
joi2019_ho_t3.cpp:48:1: warning: control reaches end of non-void function [-Wreturn-type]
48 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
763216 KB |
Output is correct |
2 |
Correct |
317 ms |
763216 KB |
Output is correct |
3 |
Correct |
309 ms |
763220 KB |
Output is correct |
4 |
Correct |
314 ms |
763120 KB |
Output is correct |
5 |
Correct |
304 ms |
763220 KB |
Output is correct |
6 |
Correct |
334 ms |
763220 KB |
Output is correct |
7 |
Correct |
312 ms |
763216 KB |
Output is correct |
8 |
Correct |
304 ms |
763216 KB |
Output is correct |
9 |
Correct |
370 ms |
763216 KB |
Output is correct |
10 |
Correct |
322 ms |
763036 KB |
Output is correct |
11 |
Correct |
319 ms |
763472 KB |
Output is correct |
12 |
Correct |
395 ms |
763128 KB |
Output is correct |
13 |
Correct |
303 ms |
763220 KB |
Output is correct |
14 |
Correct |
320 ms |
763208 KB |
Output is correct |
15 |
Correct |
310 ms |
763096 KB |
Output is correct |
16 |
Correct |
319 ms |
763220 KB |
Output is correct |
17 |
Correct |
312 ms |
763220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
763216 KB |
Output is correct |
2 |
Correct |
317 ms |
763216 KB |
Output is correct |
3 |
Correct |
309 ms |
763220 KB |
Output is correct |
4 |
Correct |
314 ms |
763120 KB |
Output is correct |
5 |
Correct |
304 ms |
763220 KB |
Output is correct |
6 |
Correct |
334 ms |
763220 KB |
Output is correct |
7 |
Correct |
312 ms |
763216 KB |
Output is correct |
8 |
Correct |
304 ms |
763216 KB |
Output is correct |
9 |
Correct |
370 ms |
763216 KB |
Output is correct |
10 |
Correct |
322 ms |
763036 KB |
Output is correct |
11 |
Correct |
319 ms |
763472 KB |
Output is correct |
12 |
Correct |
395 ms |
763128 KB |
Output is correct |
13 |
Correct |
303 ms |
763220 KB |
Output is correct |
14 |
Correct |
320 ms |
763208 KB |
Output is correct |
15 |
Correct |
310 ms |
763096 KB |
Output is correct |
16 |
Correct |
319 ms |
763220 KB |
Output is correct |
17 |
Correct |
312 ms |
763220 KB |
Output is correct |
18 |
Correct |
305 ms |
763164 KB |
Output is correct |
19 |
Correct |
348 ms |
763040 KB |
Output is correct |
20 |
Correct |
314 ms |
763168 KB |
Output is correct |
21 |
Correct |
318 ms |
763208 KB |
Output is correct |
22 |
Correct |
314 ms |
763120 KB |
Output is correct |
23 |
Correct |
312 ms |
763036 KB |
Output is correct |
24 |
Correct |
319 ms |
763216 KB |
Output is correct |
25 |
Correct |
338 ms |
763052 KB |
Output is correct |
26 |
Correct |
305 ms |
763056 KB |
Output is correct |
27 |
Correct |
324 ms |
763216 KB |
Output is correct |
28 |
Correct |
313 ms |
763192 KB |
Output is correct |
29 |
Correct |
312 ms |
763148 KB |
Output is correct |
30 |
Correct |
313 ms |
763072 KB |
Output is correct |
31 |
Correct |
312 ms |
763136 KB |
Output is correct |
32 |
Correct |
315 ms |
763332 KB |
Output is correct |
33 |
Correct |
350 ms |
763216 KB |
Output is correct |
34 |
Correct |
343 ms |
763024 KB |
Output is correct |
35 |
Correct |
355 ms |
763220 KB |
Output is correct |
36 |
Correct |
301 ms |
763320 KB |
Output is correct |
37 |
Correct |
307 ms |
763216 KB |
Output is correct |
38 |
Correct |
311 ms |
763220 KB |
Output is correct |
39 |
Correct |
304 ms |
763216 KB |
Output is correct |
40 |
Correct |
323 ms |
763220 KB |
Output is correct |
41 |
Correct |
308 ms |
763108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
315 ms |
763220 KB |
Output is correct |
2 |
Correct |
315 ms |
763216 KB |
Output is correct |
3 |
Correct |
313 ms |
763224 KB |
Output is correct |
4 |
Correct |
312 ms |
763220 KB |
Output is correct |
5 |
Correct |
308 ms |
763268 KB |
Output is correct |
6 |
Correct |
342 ms |
763048 KB |
Output is correct |
7 |
Correct |
308 ms |
763184 KB |
Output is correct |
8 |
Correct |
308 ms |
763124 KB |
Output is correct |
9 |
Correct |
312 ms |
763088 KB |
Output is correct |
10 |
Correct |
321 ms |
763220 KB |
Output is correct |
11 |
Correct |
388 ms |
763072 KB |
Output is correct |
12 |
Correct |
332 ms |
763056 KB |
Output is correct |
13 |
Correct |
345 ms |
763220 KB |
Output is correct |
14 |
Correct |
328 ms |
763220 KB |
Output is correct |
15 |
Correct |
336 ms |
763428 KB |
Output is correct |
16 |
Correct |
336 ms |
763244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
763216 KB |
Output is correct |
2 |
Correct |
317 ms |
763216 KB |
Output is correct |
3 |
Correct |
309 ms |
763220 KB |
Output is correct |
4 |
Correct |
314 ms |
763120 KB |
Output is correct |
5 |
Correct |
304 ms |
763220 KB |
Output is correct |
6 |
Correct |
334 ms |
763220 KB |
Output is correct |
7 |
Correct |
312 ms |
763216 KB |
Output is correct |
8 |
Correct |
304 ms |
763216 KB |
Output is correct |
9 |
Correct |
370 ms |
763216 KB |
Output is correct |
10 |
Correct |
322 ms |
763036 KB |
Output is correct |
11 |
Correct |
319 ms |
763472 KB |
Output is correct |
12 |
Correct |
395 ms |
763128 KB |
Output is correct |
13 |
Correct |
303 ms |
763220 KB |
Output is correct |
14 |
Correct |
320 ms |
763208 KB |
Output is correct |
15 |
Correct |
310 ms |
763096 KB |
Output is correct |
16 |
Correct |
319 ms |
763220 KB |
Output is correct |
17 |
Correct |
312 ms |
763220 KB |
Output is correct |
18 |
Correct |
305 ms |
763164 KB |
Output is correct |
19 |
Correct |
348 ms |
763040 KB |
Output is correct |
20 |
Correct |
314 ms |
763168 KB |
Output is correct |
21 |
Correct |
318 ms |
763208 KB |
Output is correct |
22 |
Correct |
314 ms |
763120 KB |
Output is correct |
23 |
Correct |
312 ms |
763036 KB |
Output is correct |
24 |
Correct |
319 ms |
763216 KB |
Output is correct |
25 |
Correct |
338 ms |
763052 KB |
Output is correct |
26 |
Correct |
305 ms |
763056 KB |
Output is correct |
27 |
Correct |
324 ms |
763216 KB |
Output is correct |
28 |
Correct |
313 ms |
763192 KB |
Output is correct |
29 |
Correct |
312 ms |
763148 KB |
Output is correct |
30 |
Correct |
313 ms |
763072 KB |
Output is correct |
31 |
Correct |
312 ms |
763136 KB |
Output is correct |
32 |
Correct |
315 ms |
763332 KB |
Output is correct |
33 |
Correct |
350 ms |
763216 KB |
Output is correct |
34 |
Correct |
343 ms |
763024 KB |
Output is correct |
35 |
Correct |
355 ms |
763220 KB |
Output is correct |
36 |
Correct |
301 ms |
763320 KB |
Output is correct |
37 |
Correct |
307 ms |
763216 KB |
Output is correct |
38 |
Correct |
311 ms |
763220 KB |
Output is correct |
39 |
Correct |
304 ms |
763216 KB |
Output is correct |
40 |
Correct |
323 ms |
763220 KB |
Output is correct |
41 |
Correct |
308 ms |
763108 KB |
Output is correct |
42 |
Correct |
315 ms |
763220 KB |
Output is correct |
43 |
Correct |
315 ms |
763216 KB |
Output is correct |
44 |
Correct |
313 ms |
763224 KB |
Output is correct |
45 |
Correct |
312 ms |
763220 KB |
Output is correct |
46 |
Correct |
308 ms |
763268 KB |
Output is correct |
47 |
Correct |
342 ms |
763048 KB |
Output is correct |
48 |
Correct |
308 ms |
763184 KB |
Output is correct |
49 |
Correct |
308 ms |
763124 KB |
Output is correct |
50 |
Correct |
312 ms |
763088 KB |
Output is correct |
51 |
Correct |
321 ms |
763220 KB |
Output is correct |
52 |
Correct |
388 ms |
763072 KB |
Output is correct |
53 |
Correct |
332 ms |
763056 KB |
Output is correct |
54 |
Correct |
345 ms |
763220 KB |
Output is correct |
55 |
Correct |
328 ms |
763220 KB |
Output is correct |
56 |
Correct |
336 ms |
763428 KB |
Output is correct |
57 |
Correct |
336 ms |
763244 KB |
Output is correct |
58 |
Correct |
368 ms |
763244 KB |
Output is correct |
59 |
Correct |
346 ms |
763112 KB |
Output is correct |
60 |
Correct |
363 ms |
763216 KB |
Output is correct |
61 |
Correct |
369 ms |
763216 KB |
Output is correct |
62 |
Correct |
324 ms |
763220 KB |
Output is correct |
63 |
Correct |
331 ms |
763112 KB |
Output is correct |
64 |
Correct |
327 ms |
763212 KB |
Output is correct |
65 |
Correct |
321 ms |
763100 KB |
Output is correct |
66 |
Correct |
344 ms |
763220 KB |
Output is correct |
67 |
Correct |
339 ms |
763220 KB |
Output is correct |
68 |
Correct |
348 ms |
763196 KB |
Output is correct |
69 |
Correct |
350 ms |
763136 KB |
Output is correct |
70 |
Correct |
353 ms |
763056 KB |
Output is correct |
71 |
Correct |
363 ms |
763288 KB |
Output is correct |
72 |
Correct |
319 ms |
763216 KB |
Output is correct |
73 |
Correct |
318 ms |
763420 KB |
Output is correct |