#include <bits/stdc++.h>
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[N];
int dp[N][N][N][3];
int a[N];
vector<int> app[3];
int i[3];
int cost(int k, int r){
i[k]--;
int 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[i];
if(c[i] == 'R')
app[0].push_back(i);
else if(c[i] == '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){
dp[i[0]][i[1]][i[2]][k] = INF;
if(i[0] + i[1] + i[2] == 0)
dp[i[0]][i[1]][i[2]][k] = 0;
if(i[k] == 0) continue;
for(int r = 0; r < 3; ++r){
if(k == r)
continue;
dp[i[0]][i[1]][i[2]][k] = min(dp[i[0]][i[1]][i[2]][k], cost(k, r));
}
}
}
}
}
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 |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
636 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 |
504 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 |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
636 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 |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
2424 KB |
Output is correct |
19 |
Correct |
5 ms |
2012 KB |
Output is correct |
20 |
Correct |
6 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
6 ms |
2168 KB |
Output is correct |
24 |
Correct |
5 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4344 KB |
Output is correct |
27 |
Correct |
6 ms |
3064 KB |
Output is correct |
28 |
Correct |
5 ms |
2424 KB |
Output is correct |
29 |
Correct |
5 ms |
2296 KB |
Output is correct |
30 |
Correct |
5 ms |
2296 KB |
Output is correct |
31 |
Correct |
5 ms |
2040 KB |
Output is correct |
32 |
Correct |
6 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 |
5 ms |
1912 KB |
Output is correct |
37 |
Correct |
5 ms |
1784 KB |
Output is correct |
38 |
Correct |
5 ms |
2168 KB |
Output is correct |
39 |
Correct |
6 ms |
2424 KB |
Output is correct |
40 |
Correct |
4 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
128 ms |
163064 KB |
Output is correct |
3 |
Correct |
126 ms |
162296 KB |
Output is correct |
4 |
Correct |
129 ms |
163064 KB |
Output is correct |
5 |
Correct |
129 ms |
163064 KB |
Output is correct |
6 |
Correct |
132 ms |
163092 KB |
Output is correct |
7 |
Correct |
127 ms |
162144 KB |
Output is correct |
8 |
Correct |
127 ms |
162256 KB |
Output is correct |
9 |
Correct |
128 ms |
161460 KB |
Output is correct |
10 |
Correct |
130 ms |
163068 KB |
Output is correct |
11 |
Correct |
131 ms |
163064 KB |
Output is correct |
12 |
Correct |
36 ms |
44152 KB |
Output is correct |
13 |
Correct |
62 ms |
77176 KB |
Output is correct |
14 |
Correct |
87 ms |
111480 KB |
Output is correct |
15 |
Correct |
127 ms |
163064 KB |
Output is correct |
16 |
Correct |
127 ms |
163064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
636 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 |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
2424 KB |
Output is correct |
19 |
Correct |
5 ms |
2012 KB |
Output is correct |
20 |
Correct |
6 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
6 ms |
2168 KB |
Output is correct |
24 |
Correct |
5 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4344 KB |
Output is correct |
27 |
Correct |
6 ms |
3064 KB |
Output is correct |
28 |
Correct |
5 ms |
2424 KB |
Output is correct |
29 |
Correct |
5 ms |
2296 KB |
Output is correct |
30 |
Correct |
5 ms |
2296 KB |
Output is correct |
31 |
Correct |
5 ms |
2040 KB |
Output is correct |
32 |
Correct |
6 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 |
5 ms |
1912 KB |
Output is correct |
37 |
Correct |
5 ms |
1784 KB |
Output is correct |
38 |
Correct |
5 ms |
2168 KB |
Output is correct |
39 |
Correct |
6 ms |
2424 KB |
Output is correct |
40 |
Correct |
4 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
2296 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
128 ms |
163064 KB |
Output is correct |
44 |
Correct |
126 ms |
162296 KB |
Output is correct |
45 |
Correct |
129 ms |
163064 KB |
Output is correct |
46 |
Correct |
129 ms |
163064 KB |
Output is correct |
47 |
Correct |
132 ms |
163092 KB |
Output is correct |
48 |
Correct |
127 ms |
162144 KB |
Output is correct |
49 |
Correct |
127 ms |
162256 KB |
Output is correct |
50 |
Correct |
128 ms |
161460 KB |
Output is correct |
51 |
Correct |
130 ms |
163068 KB |
Output is correct |
52 |
Correct |
131 ms |
163064 KB |
Output is correct |
53 |
Correct |
36 ms |
44152 KB |
Output is correct |
54 |
Correct |
62 ms |
77176 KB |
Output is correct |
55 |
Correct |
87 ms |
111480 KB |
Output is correct |
56 |
Correct |
127 ms |
163064 KB |
Output is correct |
57 |
Correct |
127 ms |
163064 KB |
Output is correct |
58 |
Execution timed out |
874 ms |
76276 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |