#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define infl 0x3f3f3f3f3f3f3f3f
#define ll long long
#define N 400
int n;
char s[N+1];
int pos[3][N+1];
//ll dp[N+1][N+1][N+1][3];
template<typename T> void chmin(T& x, T y) { x=min(x,y); }
int main()
{
scanf("%d %s",&n,s);
for(auto&x:s) x = x=='R'?0:x=='G'?1:x=='Y'?2:-1;
int c[3]={0,0,0};
for(int i=0;i<n;i++) {
pos[s[i]][c[s[i]]]=i;
c[s[i]]++;
}
vector<vector<vector<vector<ll>>>> dp(c[0]+1,vector<vector<vector<ll>>>(c[1]+1,vector<vector<ll>>(c[2]+1,vector<ll>(3,infl))));
//memset(dp,0x3f,sizeof(dp));
for(int k=0;k<3;k++) dp[0][0][0][k]=0;
for(int i0=0;i0<=c[0];i0++)
for(int i1=0;i1<=c[1];i1++)
for(int i2=0;i2<=c[2];i2++) {
for(int j=0;j<3;j++) {
int p = i0+i1+i2;
ll v=dp[i0][i1][i2][j];
//printf("dp %d %d %d %c = %d\n",i0,i1,i2,j+'a',v);
for(int k=0;k<3;k++) if(j!=k) {
int i[3]={i0,i1,i2};
i[k]++;
//printf("\t\t\t-> %d %d %d %c : %d\n",i[0],i[1],i[2],k+'a',v+abs(pos[k][i[k]-1]-p));
for(int ii=0;ii<3;ii++)if(i[ii]>c[ii])goto dont;
chmin(dp[i[0]][i[1]][i[2]][k],v+abs(pos[k][i[k]-1]-p));
dont:;
}
}
}
ll mn = infl;
for(int k=0;k<3;k++) chmin(mn,dp[c[0]][c[1]][c[2]][k]/2);
if(mn >= infl)printf("-1\n");
else printf("%lld\n",mn);
}
// then to optimize this, we can consider trying everything at once
// so dp[ra][ga][ba][current symbol] will trivialize this
// why didn't i think of this hadsfhlaksdhflkasdjalkfjslkdajflkadsj
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:25:24: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:25:32: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:26:22: warning: array subscript has type 'char' [-Wchar-subscripts]
26 | c[s[i]]++;
| ~~~^
joi2019_ho_t3.cpp:20:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d %s",&n,s);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
3932 KB |
Output is correct |
3 |
Correct |
5 ms |
3672 KB |
Output is correct |
4 |
Correct |
5 ms |
4184 KB |
Output is correct |
5 |
Correct |
5 ms |
3932 KB |
Output is correct |
6 |
Correct |
5 ms |
3932 KB |
Output is correct |
7 |
Correct |
7 ms |
3676 KB |
Output is correct |
8 |
Correct |
5 ms |
3820 KB |
Output is correct |
9 |
Correct |
5 ms |
3676 KB |
Output is correct |
10 |
Correct |
5 ms |
3932 KB |
Output is correct |
11 |
Correct |
6 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
2652 KB |
Output is correct |
15 |
Incorrect |
5 ms |
3932 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |