//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 444;
int n;
int a[maxn];
int cnt[3];
ll f[maxn][maxn][maxn][3];
int pos[3][maxn];
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
string s;
cin>>s;
rep(i,0,n) {
if (s[i]=='R') cnt[0]++,a[i]=0,pos[0][cnt[0]]=i;
else if (s[i]=='G') cnt[1]++,a[i]=1,pos[1][cnt[1]]=i;
else cnt[2]++,a[i]=2,pos[2][cnt[2]]=i;
}
memset(f,inf,sizeof(f));
rep(i,0,3) f[0][0][0][i] = 0;
rep(_,0,n)
rep(i,0,cnt[0]+1)
rep(j,0,cnt[1]+1) {
int k = _-i-j;
if (k<0) break;
if (k>cnt[2]) continue;
if (i+1<=cnt[0]) f[i+1][j][k][0] = min(f[i+1][j][k][0],min(f[i][j][k][1],f[i][j][k][2])+abs(_-pos[0][i+1]));
if (j+1<=cnt[1]) f[i][j+1][k][1] = min(f[i][j+1][k][1],min(f[i][j][k][0],f[i][j][k][2])+abs(_-pos[1][j+1]));
if (k+1<=cnt[2]) f[i][j][k+1][2] = min(f[i][j][k+1][2],min(f[i][j][k][0],f[i][j][k][1])+abs(_-pos[2][k+1]));
}
ll ans = min(f[cnt[0]][cnt[1]][cnt[2]][0],min(f[cnt[0]][cnt[1]][cnt[2]][1],f[cnt[0]][cnt[1]][cnt[2]][2]));
if (ans==f[n][n][n][0]) cout<<-1;
else cout<<ans/2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
513 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
513 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
404 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
513 ms |
1048576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |