//#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];
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 add = 0; add < 3; add++) {
ll pn = 0;
ll ind;
if (add == 0) {
ind = pos[add][i + 1];
pn = max(0, get<1>(prefCnt[ind]) - j) + max(0, get<2>(prefCnt[ind]) - k);
} else if (add == 1) {
ind = pos[add][j + 1];
pn = max(0, get<0>(prefCnt[ind]) - i) + max(0, get<2>(prefCnt[ind]) - k);
} else {
ind = pos[add][k + 1];
pn = max(0, get<0>(prefCnt[ind]) - i) + max(0, get<1>(prefCnt[ind]) - j);
}
for (int t = 0; t < 3; t++) {
if (add == t) continue;
ll cnt = dp[i][j][k][t] + pn;
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;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
809528 KB |
Output is correct |
2 |
Correct |
419 ms |
809520 KB |
Output is correct |
3 |
Correct |
419 ms |
809540 KB |
Output is correct |
4 |
Correct |
427 ms |
809540 KB |
Output is correct |
5 |
Correct |
419 ms |
809540 KB |
Output is correct |
6 |
Correct |
426 ms |
809448 KB |
Output is correct |
7 |
Correct |
420 ms |
809668 KB |
Output is correct |
8 |
Correct |
426 ms |
809440 KB |
Output is correct |
9 |
Correct |
416 ms |
809572 KB |
Output is correct |
10 |
Correct |
423 ms |
809516 KB |
Output is correct |
11 |
Correct |
427 ms |
809668 KB |
Output is correct |
12 |
Correct |
432 ms |
809492 KB |
Output is correct |
13 |
Correct |
461 ms |
809448 KB |
Output is correct |
14 |
Correct |
425 ms |
809484 KB |
Output is correct |
15 |
Correct |
411 ms |
809448 KB |
Output is correct |
16 |
Correct |
416 ms |
809632 KB |
Output is correct |
17 |
Correct |
418 ms |
809540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
809528 KB |
Output is correct |
2 |
Correct |
419 ms |
809520 KB |
Output is correct |
3 |
Correct |
419 ms |
809540 KB |
Output is correct |
4 |
Correct |
427 ms |
809540 KB |
Output is correct |
5 |
Correct |
419 ms |
809540 KB |
Output is correct |
6 |
Correct |
426 ms |
809448 KB |
Output is correct |
7 |
Correct |
420 ms |
809668 KB |
Output is correct |
8 |
Correct |
426 ms |
809440 KB |
Output is correct |
9 |
Correct |
416 ms |
809572 KB |
Output is correct |
10 |
Correct |
423 ms |
809516 KB |
Output is correct |
11 |
Correct |
427 ms |
809668 KB |
Output is correct |
12 |
Correct |
432 ms |
809492 KB |
Output is correct |
13 |
Correct |
461 ms |
809448 KB |
Output is correct |
14 |
Correct |
425 ms |
809484 KB |
Output is correct |
15 |
Correct |
411 ms |
809448 KB |
Output is correct |
16 |
Correct |
416 ms |
809632 KB |
Output is correct |
17 |
Correct |
418 ms |
809540 KB |
Output is correct |
18 |
Correct |
427 ms |
809532 KB |
Output is correct |
19 |
Correct |
417 ms |
809664 KB |
Output is correct |
20 |
Correct |
417 ms |
809704 KB |
Output is correct |
21 |
Correct |
427 ms |
809512 KB |
Output is correct |
22 |
Correct |
422 ms |
809700 KB |
Output is correct |
23 |
Correct |
420 ms |
809540 KB |
Output is correct |
24 |
Correct |
418 ms |
809572 KB |
Output is correct |
25 |
Correct |
416 ms |
809540 KB |
Output is correct |
26 |
Correct |
415 ms |
809596 KB |
Output is correct |
27 |
Correct |
420 ms |
809584 KB |
Output is correct |
28 |
Correct |
415 ms |
809540 KB |
Output is correct |
29 |
Correct |
418 ms |
809464 KB |
Output is correct |
30 |
Correct |
423 ms |
809452 KB |
Output is correct |
31 |
Correct |
435 ms |
809564 KB |
Output is correct |
32 |
Correct |
420 ms |
809452 KB |
Output is correct |
33 |
Correct |
418 ms |
809548 KB |
Output is correct |
34 |
Correct |
419 ms |
809464 KB |
Output is correct |
35 |
Correct |
426 ms |
809668 KB |
Output is correct |
36 |
Correct |
413 ms |
809536 KB |
Output is correct |
37 |
Correct |
419 ms |
809572 KB |
Output is correct |
38 |
Correct |
418 ms |
809540 KB |
Output is correct |
39 |
Correct |
417 ms |
809536 KB |
Output is correct |
40 |
Correct |
418 ms |
809500 KB |
Output is correct |
41 |
Correct |
431 ms |
809608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
423 ms |
809448 KB |
Output is correct |
2 |
Correct |
425 ms |
809540 KB |
Output is correct |
3 |
Correct |
424 ms |
809576 KB |
Output is correct |
4 |
Correct |
421 ms |
809488 KB |
Output is correct |
5 |
Correct |
427 ms |
809544 KB |
Output is correct |
6 |
Correct |
425 ms |
809548 KB |
Output is correct |
7 |
Correct |
430 ms |
809624 KB |
Output is correct |
8 |
Correct |
418 ms |
809540 KB |
Output is correct |
9 |
Correct |
427 ms |
809592 KB |
Output is correct |
10 |
Correct |
425 ms |
809668 KB |
Output is correct |
11 |
Correct |
425 ms |
809508 KB |
Output is correct |
12 |
Correct |
418 ms |
809540 KB |
Output is correct |
13 |
Correct |
422 ms |
809540 KB |
Output is correct |
14 |
Correct |
415 ms |
809608 KB |
Output is correct |
15 |
Correct |
415 ms |
809528 KB |
Output is correct |
16 |
Correct |
419 ms |
809504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
809528 KB |
Output is correct |
2 |
Correct |
419 ms |
809520 KB |
Output is correct |
3 |
Correct |
419 ms |
809540 KB |
Output is correct |
4 |
Correct |
427 ms |
809540 KB |
Output is correct |
5 |
Correct |
419 ms |
809540 KB |
Output is correct |
6 |
Correct |
426 ms |
809448 KB |
Output is correct |
7 |
Correct |
420 ms |
809668 KB |
Output is correct |
8 |
Correct |
426 ms |
809440 KB |
Output is correct |
9 |
Correct |
416 ms |
809572 KB |
Output is correct |
10 |
Correct |
423 ms |
809516 KB |
Output is correct |
11 |
Correct |
427 ms |
809668 KB |
Output is correct |
12 |
Correct |
432 ms |
809492 KB |
Output is correct |
13 |
Correct |
461 ms |
809448 KB |
Output is correct |
14 |
Correct |
425 ms |
809484 KB |
Output is correct |
15 |
Correct |
411 ms |
809448 KB |
Output is correct |
16 |
Correct |
416 ms |
809632 KB |
Output is correct |
17 |
Correct |
418 ms |
809540 KB |
Output is correct |
18 |
Correct |
427 ms |
809532 KB |
Output is correct |
19 |
Correct |
417 ms |
809664 KB |
Output is correct |
20 |
Correct |
417 ms |
809704 KB |
Output is correct |
21 |
Correct |
427 ms |
809512 KB |
Output is correct |
22 |
Correct |
422 ms |
809700 KB |
Output is correct |
23 |
Correct |
420 ms |
809540 KB |
Output is correct |
24 |
Correct |
418 ms |
809572 KB |
Output is correct |
25 |
Correct |
416 ms |
809540 KB |
Output is correct |
26 |
Correct |
415 ms |
809596 KB |
Output is correct |
27 |
Correct |
420 ms |
809584 KB |
Output is correct |
28 |
Correct |
415 ms |
809540 KB |
Output is correct |
29 |
Correct |
418 ms |
809464 KB |
Output is correct |
30 |
Correct |
423 ms |
809452 KB |
Output is correct |
31 |
Correct |
435 ms |
809564 KB |
Output is correct |
32 |
Correct |
420 ms |
809452 KB |
Output is correct |
33 |
Correct |
418 ms |
809548 KB |
Output is correct |
34 |
Correct |
419 ms |
809464 KB |
Output is correct |
35 |
Correct |
426 ms |
809668 KB |
Output is correct |
36 |
Correct |
413 ms |
809536 KB |
Output is correct |
37 |
Correct |
419 ms |
809572 KB |
Output is correct |
38 |
Correct |
418 ms |
809540 KB |
Output is correct |
39 |
Correct |
417 ms |
809536 KB |
Output is correct |
40 |
Correct |
418 ms |
809500 KB |
Output is correct |
41 |
Correct |
431 ms |
809608 KB |
Output is correct |
42 |
Correct |
423 ms |
809448 KB |
Output is correct |
43 |
Correct |
425 ms |
809540 KB |
Output is correct |
44 |
Correct |
424 ms |
809576 KB |
Output is correct |
45 |
Correct |
421 ms |
809488 KB |
Output is correct |
46 |
Correct |
427 ms |
809544 KB |
Output is correct |
47 |
Correct |
425 ms |
809548 KB |
Output is correct |
48 |
Correct |
430 ms |
809624 KB |
Output is correct |
49 |
Correct |
418 ms |
809540 KB |
Output is correct |
50 |
Correct |
427 ms |
809592 KB |
Output is correct |
51 |
Correct |
425 ms |
809668 KB |
Output is correct |
52 |
Correct |
425 ms |
809508 KB |
Output is correct |
53 |
Correct |
418 ms |
809540 KB |
Output is correct |
54 |
Correct |
422 ms |
809540 KB |
Output is correct |
55 |
Correct |
415 ms |
809608 KB |
Output is correct |
56 |
Correct |
415 ms |
809528 KB |
Output is correct |
57 |
Correct |
419 ms |
809504 KB |
Output is correct |
58 |
Correct |
478 ms |
809500 KB |
Output is correct |
59 |
Correct |
478 ms |
809548 KB |
Output is correct |
60 |
Correct |
480 ms |
809480 KB |
Output is correct |
61 |
Correct |
474 ms |
809552 KB |
Output is correct |
62 |
Correct |
433 ms |
809572 KB |
Output is correct |
63 |
Correct |
432 ms |
809540 KB |
Output is correct |
64 |
Correct |
454 ms |
809544 KB |
Output is correct |
65 |
Correct |
455 ms |
809512 KB |
Output is correct |
66 |
Correct |
480 ms |
809496 KB |
Output is correct |
67 |
Correct |
485 ms |
809680 KB |
Output is correct |
68 |
Correct |
472 ms |
809484 KB |
Output is correct |
69 |
Correct |
483 ms |
809532 KB |
Output is correct |
70 |
Correct |
482 ms |
809492 KB |
Output is correct |
71 |
Correct |
480 ms |
809544 KB |
Output is correct |
72 |
Correct |
441 ms |
809656 KB |
Output is correct |
73 |
Correct |
422 ms |
809524 KB |
Output is correct |