#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
const int INF = 1e9;
int n;
vector <int> Pos[3];
int cnt[MAXN][3];
int dp[2][MAXN][MAXN][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
char c;
cin >> c;
for(int j = 0; j < 3; j++)
{
cnt[i][j] = cnt[i - 1][j];
}
if(c == 'R')
{
cnt[i][0]++;
Pos[0].push_back(i);
}
else if(c == 'Y')
{
cnt[i][1]++;
Pos[1].push_back(i);
}
else if(c == 'G')
{
cnt[i][2]++;
Pos[2].push_back(i);
}
}
int countcolor[3];
for(int i = 0; i < 3; i++)
{
countcolor[i] = cnt[n][i];
}
for(int i = 0; i <= countcolor[0]; i++)
{
for(int j = 0; j <= countcolor[1]; j++)
{
for(int k = 0; k < 3; k++)
{
dp[0][i][j][k] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i = 1; i <= n; i++)
{
int c[3];
for(c[0] = 0; c[0] <= countcolor[0]; c[0]++)
{
for(c[1] = 0; c[1] <= countcolor[1]; c[1]++)
{
c[2] = i - c[0] - c[1];
for(int j = 0; j < 3; j++)
{
dp[i & 1][c[0]][c[1]][j] = INF;
}
if(c[2] < 0 or c[2] > countcolor[2])
{
continue;
}
for(int j = 0; j < 3; j++)
{
if(c[j])
{
int realpos = 0, id = Pos[j][c[j] - 1], t1 = INF;
for(int k = 0; k < 3; k++)
{
realpos += max(c[k], cnt[id][k]);
if(k != j)
{
c[j]--;
t1 = min(t1, dp[(i & 1) ^ 1][c[0]][c[1]][k]);
c[j]++;
}
}
dp[i & 1][c[0]][c[1]][j] = min(dp[i & 1][c[0]][c[1]][j], t1 + (realpos - i));
}
}
}
}
}
int ans = INF;
for(int i = 0; i < 3; i++)
{
ans = min(ans, dp[i & 1][countcolor[0]][countcolor[1]][i]);
}
if(ans == INF)
{
ans = -1;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |