#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]]++;
}
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));
chmin(dp[i[0]][i[1]][i[2]][k],v+abs(pos[k][i[k]-1]-p));
}
}
}
ll mn = infl;
for(int k=0;k<3;k++) chmin(mn,dp[c[0]][c[1]][c[2]][k]/2);
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 |
Runtime error |
342 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
342 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
274 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
342 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |