#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const int inf = 1e9, neg = -69420;
int n;
string s;
int dp[3][405][405][405], a[405], pos[3][405], p[3][406];
void ckmin(int &a, int b){
a = min(a, b);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
forr(i, 0, sz(s)){
if(s[i] == 'R')a[i] = 0;
else if(s[i] == 'G')a[i] = 1;
else a[i] = 2;
}
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i = 0; i < sz(s); i++){
if(a[i] == 0){
pos[0][cnt0++] = i;
}else if(a[i] == 1){
pos[1][cnt1++] = i;
}else{
pos[2][cnt2++] = i;
}
forr(j, 0, 3){
if(j == a[i]){
p[j][i + 1] = p[j][i] + 1;
}else{
p[j][i + 1] = p[j][i];
}
}
}
forr(i, 0, cnt0 + 1){
forr(j, 0, cnt1 + 1){
forr(k, 0, cnt2 + 1){
forr(last, 0, 3){
dp[last][i][j][k] = inf;
}
}
}
}
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
forr(i, 0, cnt0 + 1){
forr(j, 0, cnt1 + 1){
forr(k, 0, cnt2 + 1){
forr(last, 0, 3){
if(last != 0 && i != cnt0){
int loc = pos[0][i];
ckmin(dp[0][i + 1][j][k], dp[last][i][j][k] + max(0, p[1][loc + 1] - j) + max(0, p[2][loc + 1] - k));
}if(last != 1 && j != cnt1){
int loc = pos[1][j];
ckmin(dp[1][i][j + 1][k], dp[last][i][j][k] + max(0, p[0][loc + 1] - i) + max(0, p[2][loc + 1] - k));
}if(last != 2 && k != cnt2){
int loc = pos[2][k];
ckmin(dp[2][i][j][k + 1], dp[last][i][j][k] + max(0, p[0][loc + 1] - i) + max(0, p[1][loc + 1] - j));
}
}
}
}
}
int res = min({dp[0][cnt0][cnt1][cnt2], dp[1][cnt0][cnt1][cnt2], dp[2][cnt0][cnt1][cnt2]});
cout << ((res == inf) ? -1 : res);
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
1 ms |
2260 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2776 KB |
Output is correct |
22 |
Correct |
1 ms |
2004 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
2 ms |
1872 KB |
Output is correct |
25 |
Correct |
2 ms |
4820 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
3668 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2260 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
4692 KB |
Output is correct |
34 |
Correct |
2 ms |
4308 KB |
Output is correct |
35 |
Correct |
2 ms |
3156 KB |
Output is correct |
36 |
Correct |
1 ms |
2260 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
1 ms |
2516 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
75 ms |
194276 KB |
Output is correct |
3 |
Correct |
75 ms |
193288 KB |
Output is correct |
4 |
Correct |
74 ms |
194252 KB |
Output is correct |
5 |
Correct |
72 ms |
194240 KB |
Output is correct |
6 |
Correct |
72 ms |
194236 KB |
Output is correct |
7 |
Correct |
74 ms |
193228 KB |
Output is correct |
8 |
Correct |
71 ms |
193256 KB |
Output is correct |
9 |
Correct |
70 ms |
192340 KB |
Output is correct |
10 |
Correct |
71 ms |
194284 KB |
Output is correct |
11 |
Correct |
74 ms |
194264 KB |
Output is correct |
12 |
Correct |
22 ms |
52768 KB |
Output is correct |
13 |
Correct |
38 ms |
92184 KB |
Output is correct |
14 |
Correct |
56 ms |
132940 KB |
Output is correct |
15 |
Correct |
79 ms |
194276 KB |
Output is correct |
16 |
Correct |
80 ms |
194248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
1 ms |
2260 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2776 KB |
Output is correct |
22 |
Correct |
1 ms |
2004 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
2 ms |
1872 KB |
Output is correct |
25 |
Correct |
2 ms |
4820 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
3668 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
1 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2260 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
4692 KB |
Output is correct |
34 |
Correct |
2 ms |
4308 KB |
Output is correct |
35 |
Correct |
2 ms |
3156 KB |
Output is correct |
36 |
Correct |
1 ms |
2260 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
1 ms |
2516 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2644 KB |
Output is correct |
42 |
Correct |
1 ms |
296 KB |
Output is correct |
43 |
Correct |
75 ms |
194276 KB |
Output is correct |
44 |
Correct |
75 ms |
193288 KB |
Output is correct |
45 |
Correct |
74 ms |
194252 KB |
Output is correct |
46 |
Correct |
72 ms |
194240 KB |
Output is correct |
47 |
Correct |
72 ms |
194236 KB |
Output is correct |
48 |
Correct |
74 ms |
193228 KB |
Output is correct |
49 |
Correct |
71 ms |
193256 KB |
Output is correct |
50 |
Correct |
70 ms |
192340 KB |
Output is correct |
51 |
Correct |
71 ms |
194284 KB |
Output is correct |
52 |
Correct |
74 ms |
194264 KB |
Output is correct |
53 |
Correct |
22 ms |
52768 KB |
Output is correct |
54 |
Correct |
38 ms |
92184 KB |
Output is correct |
55 |
Correct |
56 ms |
132940 KB |
Output is correct |
56 |
Correct |
79 ms |
194276 KB |
Output is correct |
57 |
Correct |
80 ms |
194248 KB |
Output is correct |
58 |
Correct |
90 ms |
77248 KB |
Output is correct |
59 |
Correct |
80 ms |
97792 KB |
Output is correct |
60 |
Correct |
73 ms |
88980 KB |
Output is correct |
61 |
Correct |
76 ms |
85964 KB |
Output is correct |
62 |
Correct |
75 ms |
189408 KB |
Output is correct |
63 |
Correct |
77 ms |
186620 KB |
Output is correct |
64 |
Correct |
81 ms |
162976 KB |
Output is correct |
65 |
Correct |
80 ms |
141900 KB |
Output is correct |
66 |
Correct |
77 ms |
87152 KB |
Output is correct |
67 |
Correct |
73 ms |
83692 KB |
Output is correct |
68 |
Correct |
80 ms |
89792 KB |
Output is correct |
69 |
Correct |
81 ms |
86580 KB |
Output is correct |
70 |
Correct |
82 ms |
93680 KB |
Output is correct |
71 |
Correct |
77 ms |
85200 KB |
Output is correct |
72 |
Correct |
30 ms |
43988 KB |
Output is correct |
73 |
Correct |
4 ms |
2852 KB |
Output is correct |