#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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
68 ms |
162996 KB |
Output is correct |
3 |
Correct |
62 ms |
162076 KB |
Output is correct |
4 |
Correct |
57 ms |
162912 KB |
Output is correct |
5 |
Correct |
64 ms |
162956 KB |
Output is correct |
6 |
Correct |
57 ms |
162968 KB |
Output is correct |
7 |
Correct |
63 ms |
162132 KB |
Output is correct |
8 |
Correct |
134 ms |
162124 KB |
Output is correct |
9 |
Correct |
107 ms |
161352 KB |
Output is correct |
10 |
Correct |
125 ms |
162940 KB |
Output is correct |
11 |
Correct |
168 ms |
162964 KB |
Output is correct |
12 |
Correct |
19 ms |
43988 KB |
Output is correct |
13 |
Correct |
26 ms |
77144 KB |
Output is correct |
14 |
Correct |
38 ms |
111288 KB |
Output is correct |
15 |
Correct |
153 ms |
162904 KB |
Output is correct |
16 |
Correct |
60 ms |
162860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 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 |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |