#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 400 + 7;
const ll oo = 1e9 + 69;
int n, dp[maxn][maxn][maxn][3], id[3][maxn];
char a[maxn];
vector<int> pos[3];
int convert(char ch) {
if(ch == 'R') return 0;
if(ch == 'G') return 1;
return 2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n;
for(int i = 1; i <= n; i++) {
cin>>a[i];
pos[convert(a[i])].pb(i);
}
for(int i = 0; i < 3; i++) {
for(int j = 1; j <= n; j++) {
id[i][j] = lower_bound(all(pos[i]), j) - pos[i].begin();
}
}
for(int i = 0; i <= n; i++) {
for(int red = 0; red <= Size(pos[0]); red++) {
for(int green = 0; green <= Size(pos[1]); green++) {
for(int cur = 0; cur < 3; cur++) dp[i][red][green][cur] = oo;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i = 0; i < n; i++) {
for(int red = 0; red <= min(i, Size(pos[0])); red++) {
for(int green = 0; green <= min(i - red, Size(pos[1])); green++) {
int yellow = i - red - green;
if(yellow < 0 || yellow > Size(pos[2])) continue;
for(int cur = 0; cur < 3; cur++) {
int cost;
if(cur != 0 && red < Size(pos[0])) {
int p = pos[0][red];
cost = max(0, red - id[0][p]);
cost += max(0, green - id[1][p]);
cost += max(0, yellow - id[2][p]);
mini(dp[i + 1][red + 1][green][0], dp[i][red][green][cur] + cost);
}
if(cur != 1 && green < Size(pos[1])) {
cost = 0;
int p = pos[1][green];
cost = max(0, red - id[0][p]);
cost += max(0, green - id[1][p]);
cost += max(0, yellow - id[2][p]);
mini(dp[i + 1][red][green + 1][1], dp[i][red][green][cur] + cost);
}
if(cur != 2 && yellow < Size(pos[2])) {
cost = 0;
int p = pos[2][yellow];
cost = max(0, red - id[0][p]);
cost += max(0, green - id[1][p]);
cost += max(0, yellow - id[2][p]);
mini(dp[i + 1][red][green][2], dp[i][red][green][cur] + cost);
}
}
}
}
}
int ans = oo;
for(int i = 0; i < 3; i++) mini(ans, dp[n][Size(pos[0])][Size(pos[1])][i]);
cout<<(ans == oo ? -1:ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31504 KB |
Output is correct |
7 |
Correct |
4 ms |
31220 KB |
Output is correct |
8 |
Correct |
4 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31276 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
3 ms |
29188 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
4 ms |
31260 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31504 KB |
Output is correct |
7 |
Correct |
4 ms |
31220 KB |
Output is correct |
8 |
Correct |
4 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31276 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
3 ms |
29188 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
4 ms |
31260 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
18 |
Correct |
12 ms |
117340 KB |
Output is correct |
19 |
Correct |
12 ms |
117340 KB |
Output is correct |
20 |
Correct |
13 ms |
117336 KB |
Output is correct |
21 |
Correct |
13 ms |
117340 KB |
Output is correct |
22 |
Correct |
12 ms |
117340 KB |
Output is correct |
23 |
Correct |
12 ms |
117500 KB |
Output is correct |
24 |
Correct |
13 ms |
117424 KB |
Output is correct |
25 |
Correct |
12 ms |
117340 KB |
Output is correct |
26 |
Correct |
12 ms |
117340 KB |
Output is correct |
27 |
Correct |
12 ms |
117340 KB |
Output is correct |
28 |
Correct |
12 ms |
117592 KB |
Output is correct |
29 |
Correct |
12 ms |
117372 KB |
Output is correct |
30 |
Correct |
12 ms |
117340 KB |
Output is correct |
31 |
Correct |
12 ms |
117488 KB |
Output is correct |
32 |
Correct |
12 ms |
117336 KB |
Output is correct |
33 |
Correct |
11 ms |
115292 KB |
Output is correct |
34 |
Correct |
12 ms |
115292 KB |
Output is correct |
35 |
Correct |
12 ms |
113260 KB |
Output is correct |
36 |
Correct |
12 ms |
115420 KB |
Output is correct |
37 |
Correct |
11 ms |
111320 KB |
Output is correct |
38 |
Correct |
13 ms |
117336 KB |
Output is correct |
39 |
Correct |
12 ms |
117340 KB |
Output is correct |
40 |
Correct |
12 ms |
115292 KB |
Output is correct |
41 |
Correct |
11 ms |
117340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
162 ms |
778320 KB |
Output is correct |
3 |
Correct |
98 ms |
778332 KB |
Output is correct |
4 |
Correct |
98 ms |
780288 KB |
Output is correct |
5 |
Correct |
97 ms |
780372 KB |
Output is correct |
6 |
Correct |
96 ms |
780368 KB |
Output is correct |
7 |
Correct |
96 ms |
778304 KB |
Output is correct |
8 |
Correct |
97 ms |
778124 KB |
Output is correct |
9 |
Correct |
105 ms |
776276 KB |
Output is correct |
10 |
Correct |
97 ms |
780372 KB |
Output is correct |
11 |
Correct |
97 ms |
780372 KB |
Output is correct |
12 |
Correct |
43 ms |
402768 KB |
Output is correct |
13 |
Correct |
60 ms |
534112 KB |
Output is correct |
14 |
Correct |
74 ms |
644948 KB |
Output is correct |
15 |
Correct |
101 ms |
780144 KB |
Output is correct |
16 |
Correct |
97 ms |
780372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31504 KB |
Output is correct |
7 |
Correct |
4 ms |
31220 KB |
Output is correct |
8 |
Correct |
4 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31276 KB |
Output is correct |
11 |
Correct |
4 ms |
31068 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
3 ms |
29188 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
4 ms |
31260 KB |
Output is correct |
17 |
Correct |
3 ms |
29020 KB |
Output is correct |
18 |
Correct |
12 ms |
117340 KB |
Output is correct |
19 |
Correct |
12 ms |
117340 KB |
Output is correct |
20 |
Correct |
13 ms |
117336 KB |
Output is correct |
21 |
Correct |
13 ms |
117340 KB |
Output is correct |
22 |
Correct |
12 ms |
117340 KB |
Output is correct |
23 |
Correct |
12 ms |
117500 KB |
Output is correct |
24 |
Correct |
13 ms |
117424 KB |
Output is correct |
25 |
Correct |
12 ms |
117340 KB |
Output is correct |
26 |
Correct |
12 ms |
117340 KB |
Output is correct |
27 |
Correct |
12 ms |
117340 KB |
Output is correct |
28 |
Correct |
12 ms |
117592 KB |
Output is correct |
29 |
Correct |
12 ms |
117372 KB |
Output is correct |
30 |
Correct |
12 ms |
117340 KB |
Output is correct |
31 |
Correct |
12 ms |
117488 KB |
Output is correct |
32 |
Correct |
12 ms |
117336 KB |
Output is correct |
33 |
Correct |
11 ms |
115292 KB |
Output is correct |
34 |
Correct |
12 ms |
115292 KB |
Output is correct |
35 |
Correct |
12 ms |
113260 KB |
Output is correct |
36 |
Correct |
12 ms |
115420 KB |
Output is correct |
37 |
Correct |
11 ms |
111320 KB |
Output is correct |
38 |
Correct |
13 ms |
117336 KB |
Output is correct |
39 |
Correct |
12 ms |
117340 KB |
Output is correct |
40 |
Correct |
12 ms |
115292 KB |
Output is correct |
41 |
Correct |
11 ms |
117340 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
162 ms |
778320 KB |
Output is correct |
44 |
Correct |
98 ms |
778332 KB |
Output is correct |
45 |
Correct |
98 ms |
780288 KB |
Output is correct |
46 |
Correct |
97 ms |
780372 KB |
Output is correct |
47 |
Correct |
96 ms |
780368 KB |
Output is correct |
48 |
Correct |
96 ms |
778304 KB |
Output is correct |
49 |
Correct |
97 ms |
778124 KB |
Output is correct |
50 |
Correct |
105 ms |
776276 KB |
Output is correct |
51 |
Correct |
97 ms |
780372 KB |
Output is correct |
52 |
Correct |
97 ms |
780372 KB |
Output is correct |
53 |
Correct |
43 ms |
402768 KB |
Output is correct |
54 |
Correct |
60 ms |
534112 KB |
Output is correct |
55 |
Correct |
74 ms |
644948 KB |
Output is correct |
56 |
Correct |
101 ms |
780144 KB |
Output is correct |
57 |
Correct |
97 ms |
780372 KB |
Output is correct |
58 |
Correct |
119 ms |
780336 KB |
Output is correct |
59 |
Correct |
120 ms |
780308 KB |
Output is correct |
60 |
Correct |
117 ms |
776164 KB |
Output is correct |
61 |
Correct |
116 ms |
778324 KB |
Output is correct |
62 |
Correct |
102 ms |
780316 KB |
Output is correct |
63 |
Correct |
107 ms |
780236 KB |
Output is correct |
64 |
Correct |
115 ms |
780296 KB |
Output is correct |
65 |
Correct |
118 ms |
778324 KB |
Output is correct |
66 |
Correct |
116 ms |
774224 KB |
Output is correct |
67 |
Correct |
118 ms |
780168 KB |
Output is correct |
68 |
Correct |
117 ms |
778324 KB |
Output is correct |
69 |
Correct |
120 ms |
780384 KB |
Output is correct |
70 |
Correct |
116 ms |
780268 KB |
Output is correct |
71 |
Correct |
120 ms |
778252 KB |
Output is correct |
72 |
Correct |
89 ms |
778320 KB |
Output is correct |
73 |
Correct |
74 ms |
780188 KB |
Output is correct |