#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=407;
int a[N];
int DP[N][N][N][3];
vector<int>V[3];
int P[N][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++)
{
char x;
cin>>x;
if(x=='R') a[i]=0;
if(x=='G') a[i]=1;
if(x=='Y') a[i]=2;
for(int k=0;k<3;k++) P[i][k]=P[i-1][k];
V[a[i]].pb(i);
P[i][a[i]]++;
}
int x=sz(V[0]),y=sz(V[1]),z=sz(V[2]);
for(int i=0;i<=x;i++)
{
for(int j=0;j<=y;j++)
{
for(int l=0;l<=z;l++)
{
for(int k=0;k<3;k++)
{
DP[i][j][l][k]=inf;
if(i+j+l==0) DP[i][j][l][k]=0;
}
if(i>0)
{
int p=V[0][i-1];
int c=max(0,P[p][1]-j)+max(0,P[p][2]-l);
DP[i][j][l][0]=min(DP[i-1][j][l][1],DP[i-1][j][l][2])+c;
}
if(j>0)
{
int p=V[1][j-1];
int c=max(0,P[p][0]-i)+max(0,P[p][2]-l);
DP[i][j][l][1]=min(DP[i][j-1][l][0],DP[i][j-1][l][2])+c;
}
if(l>0)
{
int p=V[2][l-1];
int c=max(0,P[p][0]-i)+max(0,P[p][1]-j);
DP[i][j][l][2]=min(DP[i][j][l-1][0],DP[i][j][l-1][1])+c;
}
}
}
}
int ans=min({DP[x][y][z][0],DP[x][y][z][1],DP[x][y][z][2]});
if(ans>=inf) cout<<-1;
else cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
216 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
216 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
1 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3528 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1640 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
79 ms |
162880 KB |
Output is correct |
3 |
Correct |
68 ms |
162128 KB |
Output is correct |
4 |
Correct |
65 ms |
162900 KB |
Output is correct |
5 |
Correct |
64 ms |
162924 KB |
Output is correct |
6 |
Correct |
67 ms |
162916 KB |
Output is correct |
7 |
Correct |
65 ms |
162176 KB |
Output is correct |
8 |
Correct |
63 ms |
162144 KB |
Output is correct |
9 |
Correct |
64 ms |
161352 KB |
Output is correct |
10 |
Correct |
64 ms |
162868 KB |
Output is correct |
11 |
Correct |
65 ms |
162880 KB |
Output is correct |
12 |
Correct |
19 ms |
44084 KB |
Output is correct |
13 |
Correct |
31 ms |
77048 KB |
Output is correct |
14 |
Correct |
45 ms |
111308 KB |
Output is correct |
15 |
Correct |
64 ms |
162908 KB |
Output is correct |
16 |
Correct |
67 ms |
162876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
216 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
1 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3528 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1640 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
79 ms |
162880 KB |
Output is correct |
44 |
Correct |
68 ms |
162128 KB |
Output is correct |
45 |
Correct |
65 ms |
162900 KB |
Output is correct |
46 |
Correct |
64 ms |
162924 KB |
Output is correct |
47 |
Correct |
67 ms |
162916 KB |
Output is correct |
48 |
Correct |
65 ms |
162176 KB |
Output is correct |
49 |
Correct |
63 ms |
162144 KB |
Output is correct |
50 |
Correct |
64 ms |
161352 KB |
Output is correct |
51 |
Correct |
64 ms |
162868 KB |
Output is correct |
52 |
Correct |
65 ms |
162880 KB |
Output is correct |
53 |
Correct |
19 ms |
44084 KB |
Output is correct |
54 |
Correct |
31 ms |
77048 KB |
Output is correct |
55 |
Correct |
45 ms |
111308 KB |
Output is correct |
56 |
Correct |
64 ms |
162908 KB |
Output is correct |
57 |
Correct |
67 ms |
162876 KB |
Output is correct |
58 |
Correct |
46 ms |
76620 KB |
Output is correct |
59 |
Correct |
54 ms |
96968 KB |
Output is correct |
60 |
Correct |
52 ms |
88244 KB |
Output is correct |
61 |
Correct |
52 ms |
85264 KB |
Output is correct |
62 |
Correct |
64 ms |
160568 KB |
Output is correct |
63 |
Correct |
65 ms |
160208 KB |
Output is correct |
64 |
Correct |
67 ms |
150096 KB |
Output is correct |
65 |
Correct |
65 ms |
138492 KB |
Output is correct |
66 |
Correct |
49 ms |
86492 KB |
Output is correct |
67 |
Correct |
48 ms |
83276 KB |
Output is correct |
68 |
Correct |
51 ms |
89140 KB |
Output is correct |
69 |
Correct |
51 ms |
85864 KB |
Output is correct |
70 |
Correct |
53 ms |
92920 KB |
Output is correct |
71 |
Correct |
49 ms |
84300 KB |
Output is correct |
72 |
Correct |
21 ms |
41812 KB |
Output is correct |
73 |
Correct |
2 ms |
2892 KB |
Output is correct |