//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
typedef int ll;
typedef long double ld;
const ll K = 410, INF = 1e9;
ll n;
ll dp[K][K][K][3], pnlty[K][K][K][3];
ll cnt[3];
ll pos[3][K];
tuple<ll, ll, ll> prefCnt[K];
int main() {
fastInp;
cin >> n;
string s;
cin >> s;
int i = 1;
for (auto &c : s) {
if (c == 'R') c = 0;
if (c == 'G') c = 1;
if (c == 'Y') c = 2;
cnt[c]++;
pos[c][cnt[c]] = i;
prefCnt[i] = { cnt[0], cnt[1], cnt[2] };
i++;
}
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
for (int k = 0; k < K; k++) {
for (int t = 0; t < 3; t++) dp[i][j][k][t] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int t = 0; t < 3; t++) {
ll ind;
if (t == 0) {
ind = pos[t][i + 1];
ll a = max(0, get<0>(prefCnt[ind]) - i);
ll b = max(0, get<1>(prefCnt[ind]) - j);
ll c = max(0, get<2>(prefCnt[ind]) - k);
pnlty[i][j][k][t] = b + c;
} else if (t == 1) {
ind = pos[t][j + 1];
ll a = max(0, get<0>(prefCnt[ind]) - i);
ll b = max(0, get<1>(prefCnt[ind]) - j);
ll c = max(0, get<2>(prefCnt[ind]) - k);
pnlty[i][j][k][t] = a + c;
} else {
ind = pos[t][k + 1];
ll a = max(0, get<0>(prefCnt[ind]) - i);
ll b = max(0, get<1>(prefCnt[ind]) - j);
ll c = max(0, get<2>(prefCnt[ind]) - k);
pnlty[i][j][k][t] = a + b;
}
}
}
}
}
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int t = 0; t < 3; t++) {
for (int add = 0; add < 3; add++) {
if (add == t) continue;
ll cnt = dp[i][j][k][t] + pnlty[i][j][k][add];
if (add == 0) {
dp[i + 1][j][k][add] = min(dp[i + 1][j][k][add], cnt);
} else if (add == 1) {
dp[i][j + 1][k][add] = min(dp[i][j + 1][k][add], cnt);
} else {
dp[i][j][k + 1][add] = min(dp[i][j][k + 1][add], cnt);
}
}
}
}
}
}
ll a = INF;
for (int i = 0; i < 3; i++) a = min(a, dp[cnt[0]][cnt[1]][cnt[2]][i]);
if (a >= INF) {
cout << "-1\n";
} else {
cout << a;
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:39:7: warning: array subscript has type 'char' [-Wchar-subscripts]
39 | cnt[c]++;
| ^
joi2019_ho_t3.cpp:40:7: warning: array subscript has type 'char' [-Wchar-subscripts]
40 | pos[c][cnt[c]] = i;
| ^
joi2019_ho_t3.cpp:40:14: warning: array subscript has type 'char' [-Wchar-subscripts]
40 | pos[c][cnt[c]] = i;
| ^
joi2019_ho_t3.cpp:64:10: warning: unused variable 'a' [-Wunused-variable]
64 | ll a = max(0, get<0>(prefCnt[ind]) - i);
| ^
joi2019_ho_t3.cpp:71:10: warning: unused variable 'b' [-Wunused-variable]
71 | ll b = max(0, get<1>(prefCnt[ind]) - j);
| ^
joi2019_ho_t3.cpp:78:10: warning: unused variable 'c' [-Wunused-variable]
78 | ll c = max(0, get<2>(prefCnt[ind]) - k);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
452 ms |
809456 KB |
Output is correct |
2 |
Correct |
469 ms |
809524 KB |
Output is correct |
3 |
Correct |
463 ms |
809460 KB |
Output is correct |
4 |
Correct |
441 ms |
809580 KB |
Output is correct |
5 |
Correct |
442 ms |
809668 KB |
Output is correct |
6 |
Correct |
441 ms |
809640 KB |
Output is correct |
7 |
Correct |
431 ms |
809796 KB |
Output is correct |
8 |
Correct |
445 ms |
809776 KB |
Output is correct |
9 |
Correct |
442 ms |
809728 KB |
Output is correct |
10 |
Correct |
452 ms |
809832 KB |
Output is correct |
11 |
Correct |
455 ms |
809796 KB |
Output is correct |
12 |
Correct |
438 ms |
809668 KB |
Output is correct |
13 |
Correct |
436 ms |
809544 KB |
Output is correct |
14 |
Correct |
445 ms |
809636 KB |
Output is correct |
15 |
Correct |
438 ms |
809616 KB |
Output is correct |
16 |
Correct |
432 ms |
809668 KB |
Output is correct |
17 |
Correct |
438 ms |
809668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
452 ms |
809456 KB |
Output is correct |
2 |
Correct |
469 ms |
809524 KB |
Output is correct |
3 |
Correct |
463 ms |
809460 KB |
Output is correct |
4 |
Correct |
441 ms |
809580 KB |
Output is correct |
5 |
Correct |
442 ms |
809668 KB |
Output is correct |
6 |
Correct |
441 ms |
809640 KB |
Output is correct |
7 |
Correct |
431 ms |
809796 KB |
Output is correct |
8 |
Correct |
445 ms |
809776 KB |
Output is correct |
9 |
Correct |
442 ms |
809728 KB |
Output is correct |
10 |
Correct |
452 ms |
809832 KB |
Output is correct |
11 |
Correct |
455 ms |
809796 KB |
Output is correct |
12 |
Correct |
438 ms |
809668 KB |
Output is correct |
13 |
Correct |
436 ms |
809544 KB |
Output is correct |
14 |
Correct |
445 ms |
809636 KB |
Output is correct |
15 |
Correct |
438 ms |
809616 KB |
Output is correct |
16 |
Correct |
432 ms |
809668 KB |
Output is correct |
17 |
Correct |
438 ms |
809668 KB |
Output is correct |
18 |
Correct |
453 ms |
811528 KB |
Output is correct |
19 |
Correct |
456 ms |
811084 KB |
Output is correct |
20 |
Correct |
444 ms |
811468 KB |
Output is correct |
21 |
Correct |
438 ms |
811588 KB |
Output is correct |
22 |
Correct |
448 ms |
810988 KB |
Output is correct |
23 |
Correct |
449 ms |
811332 KB |
Output is correct |
24 |
Correct |
441 ms |
810948 KB |
Output is correct |
25 |
Correct |
441 ms |
813204 KB |
Output is correct |
26 |
Correct |
447 ms |
813360 KB |
Output is correct |
27 |
Correct |
447 ms |
812256 KB |
Output is correct |
28 |
Correct |
447 ms |
811576 KB |
Output is correct |
29 |
Correct |
460 ms |
811444 KB |
Output is correct |
30 |
Correct |
492 ms |
811472 KB |
Output is correct |
31 |
Execution timed out |
502 ms |
811180 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
499 ms |
809512 KB |
Output is correct |
2 |
Execution timed out |
512 ms |
972148 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
452 ms |
809456 KB |
Output is correct |
2 |
Correct |
469 ms |
809524 KB |
Output is correct |
3 |
Correct |
463 ms |
809460 KB |
Output is correct |
4 |
Correct |
441 ms |
809580 KB |
Output is correct |
5 |
Correct |
442 ms |
809668 KB |
Output is correct |
6 |
Correct |
441 ms |
809640 KB |
Output is correct |
7 |
Correct |
431 ms |
809796 KB |
Output is correct |
8 |
Correct |
445 ms |
809776 KB |
Output is correct |
9 |
Correct |
442 ms |
809728 KB |
Output is correct |
10 |
Correct |
452 ms |
809832 KB |
Output is correct |
11 |
Correct |
455 ms |
809796 KB |
Output is correct |
12 |
Correct |
438 ms |
809668 KB |
Output is correct |
13 |
Correct |
436 ms |
809544 KB |
Output is correct |
14 |
Correct |
445 ms |
809636 KB |
Output is correct |
15 |
Correct |
438 ms |
809616 KB |
Output is correct |
16 |
Correct |
432 ms |
809668 KB |
Output is correct |
17 |
Correct |
438 ms |
809668 KB |
Output is correct |
18 |
Correct |
453 ms |
811528 KB |
Output is correct |
19 |
Correct |
456 ms |
811084 KB |
Output is correct |
20 |
Correct |
444 ms |
811468 KB |
Output is correct |
21 |
Correct |
438 ms |
811588 KB |
Output is correct |
22 |
Correct |
448 ms |
810988 KB |
Output is correct |
23 |
Correct |
449 ms |
811332 KB |
Output is correct |
24 |
Correct |
441 ms |
810948 KB |
Output is correct |
25 |
Correct |
441 ms |
813204 KB |
Output is correct |
26 |
Correct |
447 ms |
813360 KB |
Output is correct |
27 |
Correct |
447 ms |
812256 KB |
Output is correct |
28 |
Correct |
447 ms |
811576 KB |
Output is correct |
29 |
Correct |
460 ms |
811444 KB |
Output is correct |
30 |
Correct |
492 ms |
811472 KB |
Output is correct |
31 |
Execution timed out |
502 ms |
811180 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |