#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 405
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
char c;
int dp[N][N][N][3];
int a[N];
vector<int> app[3];
int i[3];
int cost(int k){
i[k]--;
int res = INF;
for(int r = 0; r < 3; ++r)
if(r != k)
res = min(res, dp[i[0]][i[1]][i[2]][r]);
int v = app[k][i[k]];
i[k]++;
int w = v;
for(int j = 0; j < 3; j++){
int a = upper_bound(app[j].begin(), app[j].begin() + i[j], v) - app[j].begin();
w += max(0, i[j] - a);
}
return res + abs(i[0] + i[1] + i[2] - w);
}
int s[3];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
cin>>c;
if(c == 'R')
app[0].push_back(i);
else if(c == 'G')
app[1].push_back(i);
else
app[2].push_back(i);
}
for(int i = 0; i < 3; ++i)
s[i] = app[i].size();
for(i[0] = 0; i[0] <= s[0]; ++i[0]){
for(i[1] = 0; i[1] <= s[1]; ++i[1]){
for(i[2] = 0; i[2] <= s[2]; ++i[2]){
for(int k = 0; k < 3; ++k){
if(i[0] + i[1] + i[2] == 0)
dp[i[0]][i[1]][i[2]][k] = 0;
else if(i[k] == 0)
dp[i[0]][i[1]][i[2]][k] = INF;
else
dp[i[0]][i[1]][i[2]][k] = cost(k);
}
}
}
}
int res = min(dp[s[0]][s[1]][s[2]][0], min(dp[s[0]][s[1]][s[2]][1], dp[s[0]][s[1]][s[2]][2]));
if(res == 0x3f3f3f3f)
cout<<-1;
else
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
14 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
14 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
2424 KB |
Output is correct |
19 |
Correct |
4 ms |
1912 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
21 |
Correct |
5 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
1656 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
1660 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
3192 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2268 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
5 ms |
2396 KB |
Output is correct |
33 |
Correct |
5 ms |
3960 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
1884 KB |
Output is correct |
37 |
Correct |
4 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
123 ms |
162960 KB |
Output is correct |
3 |
Correct |
123 ms |
162188 KB |
Output is correct |
4 |
Correct |
125 ms |
163000 KB |
Output is correct |
5 |
Correct |
126 ms |
163064 KB |
Output is correct |
6 |
Correct |
134 ms |
162992 KB |
Output is correct |
7 |
Correct |
126 ms |
162268 KB |
Output is correct |
8 |
Correct |
126 ms |
162296 KB |
Output is correct |
9 |
Correct |
123 ms |
161400 KB |
Output is correct |
10 |
Correct |
130 ms |
163064 KB |
Output is correct |
11 |
Correct |
140 ms |
163092 KB |
Output is correct |
12 |
Correct |
39 ms |
44156 KB |
Output is correct |
13 |
Correct |
62 ms |
77176 KB |
Output is correct |
14 |
Correct |
86 ms |
111480 KB |
Output is correct |
15 |
Correct |
136 ms |
163064 KB |
Output is correct |
16 |
Correct |
139 ms |
162936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
14 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
2424 KB |
Output is correct |
19 |
Correct |
4 ms |
1912 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
21 |
Correct |
5 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
1656 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
1660 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
3192 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2268 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
5 ms |
2396 KB |
Output is correct |
33 |
Correct |
5 ms |
3960 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
4 ms |
1884 KB |
Output is correct |
37 |
Correct |
4 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
123 ms |
162960 KB |
Output is correct |
44 |
Correct |
123 ms |
162188 KB |
Output is correct |
45 |
Correct |
125 ms |
163000 KB |
Output is correct |
46 |
Correct |
126 ms |
163064 KB |
Output is correct |
47 |
Correct |
134 ms |
162992 KB |
Output is correct |
48 |
Correct |
126 ms |
162268 KB |
Output is correct |
49 |
Correct |
126 ms |
162296 KB |
Output is correct |
50 |
Correct |
123 ms |
161400 KB |
Output is correct |
51 |
Correct |
130 ms |
163064 KB |
Output is correct |
52 |
Correct |
140 ms |
163092 KB |
Output is correct |
53 |
Correct |
39 ms |
44156 KB |
Output is correct |
54 |
Correct |
62 ms |
77176 KB |
Output is correct |
55 |
Correct |
86 ms |
111480 KB |
Output is correct |
56 |
Correct |
136 ms |
163064 KB |
Output is correct |
57 |
Correct |
139 ms |
162936 KB |
Output is correct |
58 |
Correct |
449 ms |
76240 KB |
Output is correct |
59 |
Correct |
469 ms |
96632 KB |
Output is correct |
60 |
Correct |
462 ms |
87832 KB |
Output is correct |
61 |
Correct |
462 ms |
84868 KB |
Output is correct |
62 |
Correct |
154 ms |
160760 KB |
Output is correct |
63 |
Correct |
163 ms |
160376 KB |
Output is correct |
64 |
Correct |
288 ms |
150212 KB |
Output is correct |
65 |
Correct |
377 ms |
138744 KB |
Output is correct |
66 |
Correct |
343 ms |
86136 KB |
Output is correct |
67 |
Correct |
344 ms |
82924 KB |
Output is correct |
68 |
Correct |
380 ms |
88788 KB |
Output is correct |
69 |
Correct |
412 ms |
85624 KB |
Output is correct |
70 |
Correct |
397 ms |
92692 KB |
Output is correct |
71 |
Correct |
428 ms |
83960 KB |
Output is correct |
72 |
Correct |
134 ms |
41720 KB |
Output is correct |
73 |
Correct |
24 ms |
2936 KB |
Output is correct |