#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
typedef tuple<int, int, int> t3;
int N;
int X[410];
char buf[410];
int lc[3][410][3], cnt[3], w[3][410];
int dp[410][205][135][3];
void upd(int &a, int b) {
if(a > b) a = b;
}
int main() {
scanf("%d", &N);
scanf("%s", buf + 1);
for(int i=1;i<=N;i++) X[i] = (buf[i] == 'Y' ? 0 : buf[i] == 'G' ? 1 : 2);
pii tcnt[3] = {};
rep(i, 3) tcnt[i].Se = i;
for(int i=1;i<=N;i++) tcnt[X[i]].Fi++;
sort(tcnt, tcnt + 3);
for(int i=1;i<=N;i++) {
rep(u, 3) if(X[i] == tcnt[u].Se) { X[i] = 2 - u; break; }
}
for(int i=1;i<=N;i++) {
int v = X[i];
cnt[v]++;
w[v][cnt[v]] = i;
rep(a, 3) lc[v][cnt[v]][a] = cnt[a];
}
for(int i=0;i<=cnt[0];i++) for(int j=0;j<=cnt[1];j++) for(int k=0;k<=cnt[2];k++) rep(v, 3) dp[i][j][k][v] = 1e9;
rep(u, 3) dp[0][0][0][u] = 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++) rep(v, 3) {
int val = dp[i][j][k][v];
if(val > 1e6) continue;
if(i < cnt[0] && v != 0) {
int r = w[0][i+1] - 1;
r -= min(lc[0][i+1][0], i) + min(lc[0][i+1][1], j) + min(lc[0][i+1][2], k);
upd(dp[i+1][j][k][0], r + val);
}
if(j < cnt[1] && v != 1) {
int r = w[1][j+1] - 1;
r -= min(lc[1][j+1][0], i) + min(lc[1][j+1][1], j) + min(lc[1][j+1][2], k);
upd(dp[i][j+1][k][1], r + val);
}
if(k < cnt[2] && v != 2) {
int r = w[2][k+1] - 1;
r -= min(lc[2][k+1][0], i) + min(lc[2][k+1][1], j) + min(lc[2][k+1][2], k);
upd(dp[i][j][k+1][2], r + val);
}
}
int ans = 1e9;
rep(u, 3) ans = min(ans, dp[cnt[0]][cnt[1]][cnt[2]][u]);
if(ans < 1e9) printf("%d\n", ans);
else puts("-1");
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", buf + 1);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
1152 KB |
Output is correct |
19 |
Correct |
3 ms |
1180 KB |
Output is correct |
20 |
Correct |
3 ms |
1280 KB |
Output is correct |
21 |
Correct |
3 ms |
1152 KB |
Output is correct |
22 |
Correct |
3 ms |
1408 KB |
Output is correct |
23 |
Correct |
3 ms |
1280 KB |
Output is correct |
24 |
Correct |
3 ms |
1536 KB |
Output is correct |
25 |
Correct |
3 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
3 ms |
1408 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
3 ms |
1152 KB |
Output is correct |
31 |
Correct |
3 ms |
1280 KB |
Output is correct |
32 |
Correct |
3 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1792 KB |
Output is correct |
34 |
Correct |
4 ms |
1664 KB |
Output is correct |
35 |
Correct |
4 ms |
1280 KB |
Output is correct |
36 |
Correct |
3 ms |
1280 KB |
Output is correct |
37 |
Correct |
4 ms |
1280 KB |
Output is correct |
38 |
Correct |
3 ms |
1280 KB |
Output is correct |
39 |
Correct |
4 ms |
1152 KB |
Output is correct |
40 |
Correct |
3 ms |
1792 KB |
Output is correct |
41 |
Correct |
3 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
51 ms |
64888 KB |
Output is correct |
3 |
Correct |
53 ms |
64572 KB |
Output is correct |
4 |
Correct |
50 ms |
64888 KB |
Output is correct |
5 |
Correct |
53 ms |
64924 KB |
Output is correct |
6 |
Correct |
52 ms |
65020 KB |
Output is correct |
7 |
Correct |
51 ms |
64504 KB |
Output is correct |
8 |
Correct |
53 ms |
64504 KB |
Output is correct |
9 |
Correct |
53 ms |
64248 KB |
Output is correct |
10 |
Correct |
52 ms |
64888 KB |
Output is correct |
11 |
Correct |
52 ms |
64860 KB |
Output is correct |
12 |
Correct |
17 ms |
17792 KB |
Output is correct |
13 |
Correct |
26 ms |
30840 KB |
Output is correct |
14 |
Correct |
34 ms |
44416 KB |
Output is correct |
15 |
Correct |
56 ms |
65016 KB |
Output is correct |
16 |
Correct |
55 ms |
64888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
1152 KB |
Output is correct |
19 |
Correct |
3 ms |
1180 KB |
Output is correct |
20 |
Correct |
3 ms |
1280 KB |
Output is correct |
21 |
Correct |
3 ms |
1152 KB |
Output is correct |
22 |
Correct |
3 ms |
1408 KB |
Output is correct |
23 |
Correct |
3 ms |
1280 KB |
Output is correct |
24 |
Correct |
3 ms |
1536 KB |
Output is correct |
25 |
Correct |
3 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
3 ms |
1408 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
3 ms |
1152 KB |
Output is correct |
30 |
Correct |
3 ms |
1152 KB |
Output is correct |
31 |
Correct |
3 ms |
1280 KB |
Output is correct |
32 |
Correct |
3 ms |
1152 KB |
Output is correct |
33 |
Correct |
4 ms |
1792 KB |
Output is correct |
34 |
Correct |
4 ms |
1664 KB |
Output is correct |
35 |
Correct |
4 ms |
1280 KB |
Output is correct |
36 |
Correct |
3 ms |
1280 KB |
Output is correct |
37 |
Correct |
4 ms |
1280 KB |
Output is correct |
38 |
Correct |
3 ms |
1280 KB |
Output is correct |
39 |
Correct |
4 ms |
1152 KB |
Output is correct |
40 |
Correct |
3 ms |
1792 KB |
Output is correct |
41 |
Correct |
3 ms |
1280 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
51 ms |
64888 KB |
Output is correct |
44 |
Correct |
53 ms |
64572 KB |
Output is correct |
45 |
Correct |
50 ms |
64888 KB |
Output is correct |
46 |
Correct |
53 ms |
64924 KB |
Output is correct |
47 |
Correct |
52 ms |
65020 KB |
Output is correct |
48 |
Correct |
51 ms |
64504 KB |
Output is correct |
49 |
Correct |
53 ms |
64504 KB |
Output is correct |
50 |
Correct |
53 ms |
64248 KB |
Output is correct |
51 |
Correct |
52 ms |
64888 KB |
Output is correct |
52 |
Correct |
52 ms |
64860 KB |
Output is correct |
53 |
Correct |
17 ms |
17792 KB |
Output is correct |
54 |
Correct |
26 ms |
30840 KB |
Output is correct |
55 |
Correct |
34 ms |
44416 KB |
Output is correct |
56 |
Correct |
56 ms |
65016 KB |
Output is correct |
57 |
Correct |
55 ms |
64888 KB |
Output is correct |
58 |
Correct |
63 ms |
31608 KB |
Output is correct |
59 |
Correct |
62 ms |
32888 KB |
Output is correct |
60 |
Correct |
66 ms |
30464 KB |
Output is correct |
61 |
Correct |
62 ms |
29824 KB |
Output is correct |
62 |
Correct |
54 ms |
63236 KB |
Output is correct |
63 |
Correct |
57 ms |
62328 KB |
Output is correct |
64 |
Correct |
76 ms |
54520 KB |
Output is correct |
65 |
Correct |
62 ms |
47708 KB |
Output is correct |
66 |
Correct |
56 ms |
29568 KB |
Output is correct |
67 |
Correct |
70 ms |
34172 KB |
Output is correct |
68 |
Correct |
69 ms |
30260 KB |
Output is correct |
69 |
Correct |
60 ms |
30456 KB |
Output is correct |
70 |
Correct |
58 ms |
31608 KB |
Output is correct |
71 |
Correct |
69 ms |
33536 KB |
Output is correct |
72 |
Correct |
34 ms |
35832 KB |
Output is correct |
73 |
Correct |
45 ms |
50468 KB |
Output is correct |