#include <bits/stdc++.h>
#define fi first
#define se second
#define ryan bear
#define now mem[co][r][g][y]
#define all(V) ((V).begin()), ((V).end())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef vector<int> vim;
typedef vector<ll> vlm;
int n, ar[410], mem[3][410][410][410], cnt[3][410];
char s[410];
vim c[3];
int f(int co, int r, int g, int y) {
if (!(r+g+y)) return 0;
if (now!=-1) return now;
now=(1<<30);
if (r&&co==0) now = min( now , min(f(1, r-1, g, y), f(2, r-1, g, y))+abs(g-cnt[1][c[0][r-1]])+abs(y-cnt[2][c[0][r-1]]));
if (g&&co==1) now = min( now , min(f(0, r, g-1, y), f(2, r, g-1, y))+abs(r-cnt[0][c[1][g-1]])+abs(y-cnt[2][c[1][g-1]]));
if (y&&co==2) now = min( now , min(f(0, r, g, y-1), f(1, r, g, y-1))+abs(r-cnt[0][c[2][y-1]])+abs(g-cnt[1][c[2][y-1]]));
return now;
}
int main() {
scanf("%d %s", &n, s);
for (int i=1; i<=n; i++) ar[i]=(s[i-1]=='R'?0:(s[i-1]=='G'?1:2));
for (int i=1; i<=n; i++) c[ar[i]].push_back(i);
for (int i=1; i<=n; i++) {
for (int j=0; j<3; j++) cnt[j][i]=cnt[j][i-1];
cnt[ar[i]][i]++;
}
for (int i=0; i<3; i++) for (int j=0; j<401; j++) for (int k=0; k<401; k++) for (int l=0; l<401; l++) mem[i][j][k][l]=-1;
int ans=(1<<30);
for (int i=0; i<3; i++) if (c[i].size()) ans=min(ans, f(i, cnt[0][n], cnt[1][n], cnt[2][n]));
if (ans==(1<<30)) {puts("-1"); return 0;}
printf("%d\n", ans/2);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %s", &n, s);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
865 ms |
779264 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
865 ms |
779264 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
691 ms |
779192 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
865 ms |
779264 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |