//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 400 + 1;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,p[3][N];
vector <int> g[3];
int cnt1 , cnt2 , cnt3;
string s,t;
vector <string> q;
void Gold(){
cin >> n >> s;
s = '+' + s;
t.pb('R');
t.pb('Y');
t.pb('G');
for(int i = 1 ; i <= n ; i++){
if(s[i] == 'R'){
cnt1++;
}
if(s[i] == 'Y'){
cnt2++;
}
if(s[i] == 'G'){
cnt3++;
}
for(int j = 0 ; j < 3 ; j++){
p[j][i] = p[j][i - 1] + (s[i] == t[j]);
}
}
int dp[n + 1][cnt1 + 1][cnt2 + 1][3];
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j <= cnt1 ; j++){
for(int k = 0 ; k <= cnt2 ; k++){
for(int y = 0 ; y < 3 ; y++){
dp[i][j][k][y] = INF;
}
}
}
}
for(int y = 0 ; y < 3 ; y++){
dp[0][0][0][y] = 0;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j < 3 ; j++){
if(s[i] == t[j]){
g[j].pb(i);
}
}
}
// cout << "ok";
for(int i = 1 ; i <= n ; i++){
for(int a = 0 ; a <= cnt1 ; a++){
for(int b = 0 ; b <=cnt2 ; b++){
if(a+b>i) continue;
int c = i - (a + b);
if(c > cnt3) continue;
if(a){
int pos = g[0][a - 1];
int cnt = max(0ll,(b - p[1][pos])) + max((c - p[2][pos]) , 0ll);
dp[i][a][b][0] = min(dp[i - 1][a - 1][b][1] , dp[i - 1][a - 1][b][2]) + max(0ll , (pos + cnt) - i);
}
if(b){
int pos = g[1][b - 1];
int cnt = max(a - p[0][pos] , 0ll) + max(c - p[2][pos] , 0ll);
dp[i][a][b][1] = min(dp[i - 1][a][b - 1][0] , dp[i - 1][a][b - 1][2]) + max(0ll , (pos + cnt) - i);
}
if(c){
int pos = g[2][c - 1];
int cnt = max(0ll , a - p[0][pos]) + max(0ll , b - p[1][pos]);
dp[i][a][b][2] = min(dp[i - 1][a][b][0] , dp[i - 1][a][b][1]) + max(0ll , (pos + cnt) - i);
}
// cout << i << ' ' << a << ' ' << b << ' ' << c << '\n';
}
}
}
int ans = INF;
for(int i = 0 ; i < 3 ; i++){
ans = min(ans , dp[n][cnt1][cnt2][i]);
}
if(ans == INF){
cout << "-1\n";
return;
}
cout << ans;
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
// ios_base::sync_with_stdio(0);
// cin.tie(0);
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
0 ms |
872 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
948 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
0 ms |
860 KB |
Output is correct |
38 |
Correct |
0 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
2144 KB |
Output is correct |
3 |
Correct |
1 ms |
2140 KB |
Output is correct |
4 |
Correct |
2 ms |
2148 KB |
Output is correct |
5 |
Correct |
2 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2140 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
2 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
2140 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Correct |
3 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
2 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
0 ms |
872 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
948 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
0 ms |
860 KB |
Output is correct |
38 |
Correct |
0 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
600 KB |
Output is correct |
43 |
Correct |
2 ms |
2144 KB |
Output is correct |
44 |
Correct |
1 ms |
2140 KB |
Output is correct |
45 |
Correct |
2 ms |
2148 KB |
Output is correct |
46 |
Correct |
2 ms |
2140 KB |
Output is correct |
47 |
Correct |
1 ms |
2140 KB |
Output is correct |
48 |
Correct |
2 ms |
2140 KB |
Output is correct |
49 |
Correct |
2 ms |
2140 KB |
Output is correct |
50 |
Correct |
1 ms |
2140 KB |
Output is correct |
51 |
Correct |
1 ms |
2140 KB |
Output is correct |
52 |
Correct |
3 ms |
2140 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
1116 KB |
Output is correct |
55 |
Correct |
1 ms |
1628 KB |
Output is correct |
56 |
Correct |
1 ms |
2140 KB |
Output is correct |
57 |
Correct |
2 ms |
2140 KB |
Output is correct |
58 |
Correct |
117 ms |
176508 KB |
Output is correct |
59 |
Correct |
110 ms |
162868 KB |
Output is correct |
60 |
Correct |
115 ms |
174424 KB |
Output is correct |
61 |
Correct |
117 ms |
171604 KB |
Output is correct |
62 |
Correct |
7 ms |
10328 KB |
Output is correct |
63 |
Correct |
12 ms |
16988 KB |
Output is correct |
64 |
Correct |
46 ms |
62252 KB |
Output is correct |
65 |
Correct |
64 ms |
87380 KB |
Output is correct |
66 |
Correct |
118 ms |
169300 KB |
Output is correct |
67 |
Correct |
99 ms |
146008 KB |
Output is correct |
68 |
Correct |
114 ms |
167504 KB |
Output is correct |
69 |
Correct |
120 ms |
175720 KB |
Output is correct |
70 |
Correct |
104 ms |
160340 KB |
Output is correct |
71 |
Correct |
123 ms |
193104 KB |
Output is correct |
72 |
Correct |
123 ms |
204628 KB |
Output is correct |
73 |
Correct |
9 ms |
13916 KB |
Output is correct |