This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N;
int memo[410][410][410][3];
int rp[410],gp[410],yp[410];
int R;
int G;
int Y;
char lst[410];
const int inf = 1100100100;
bool visited[410];
int root[410][410][3];
int main(){
scanf(" %d",&N);
for(int i = 0; i < N; i++){
scanf(" %c",&lst[i]);
if(lst[i] == 'R'){
R++;
}
else if(lst[i] == 'G'){
G++;
}
else if(lst[i] == 'Y'){
Y++;
}
}
memo[R][G][Y][0] = 0;
memo[R][G][Y][1] = 0;
memo[R][G][Y][2] = 0;
int it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'R'){
rp[it] = i;
it++;
}
}
it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'G'){
gp[it] = i;
it++;
}
}
it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'Y'){
yp[it] = i;
it++;
}
}
for(int i = 0; i <= N; i++){
root[R][i][0] = 0;
root[G][i][0] = 0;
root[Y][i][0] = 0;
}
for(int r = R - 1; r >= 0; r--){
for(int i = 0; i <= N; i++){
root[r][i][0] = root[r + 1][i][0];
if(i >= rp[r]) root[r][i][0]++;
}
}
for(int g = G - 1; g >= 0; g--){
for(int i = 0; i <= N; i++){
root[g][i][1] = root[g + 1][i][1];
if(i >= gp[g]) root[g][i][1]++;
}
}
for(int y = Y - 1; y >= 0; y--){
for(int i = 0; i <= N; i++){
root[y][i][2] = root[y + 1][i][2];
if(i >= yp[y]) root[y][i][2]++;
}
}
for(int i = N - 1; i >= 0; i--){
for(int r = 0; r <= min(R,i); r++){
for(int g = 0; g <= min(G,i - r); g++){
for(int y = 0; y <= min(Y,i - r - g); y++){
int j = r + g + y;
if(r == R || min(memo[r + 1][g][y][1], memo[r + 1][g][y][2]) == inf){
memo[r][g][y][0] = inf;
}
else{
memo[r][g][y][0] = min(memo[r + 1][g][y][1], memo[r + 1][g][y][2]) + j-rp[r]+root[r+1][rp[r]][0] + root[g][rp[r]][1] + root[y][rp[r]][2];
}
if(g == G || min(memo[r][g + 1][y][0], memo[r][g + 1][y][2]) == inf){
memo[r][g][y][1] = inf;
}
else{
memo[r][g][y][1] = min(memo[r][g + 1][y][0], memo[r][g + 1][y][2])+ j-gp[g]+root[r][gp[g]][0] + root[g+1][gp[g]][1] + root[y][gp[g]][2];
}
if(y == Y || min(memo[r][g][y + 1][0], memo[r][g][y + 1][1]) == inf){
memo[r][g][y][2] = inf;
}
else{
memo[r][g][y][2] = min(memo[r][g][y + 1][0], memo[r][g][y + 1][1])+ j-yp[y]+root[r][yp[y]][0] + root[g][yp[y]][1] + root[y+1][yp[y]][2];
}
}
}
}
}
if(min(memo[0][0][0][0],min(memo[0][0][0][1],memo[0][0][0][2])) != inf){
printf("%d",min(memo[0][0][0][0],min(memo[0][0][0][1],memo[0][0][0][2])));
}
else{
printf("-1");
}
}
/*
6
GGGGRRR
GRGRGRG
RGRGYG
*/
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %c",&lst[i]);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |