//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;
}
rep(_,1,n+1)
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;
rep(kk,0,3) f[i][j][k][kk]=1e17;
}
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==1e17) cout<<-1;
else cout<<ans/2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
82 ms |
163496 KB |
Output is correct |
3 |
Correct |
79 ms |
162712 KB |
Output is correct |
4 |
Correct |
78 ms |
163536 KB |
Output is correct |
5 |
Correct |
80 ms |
163532 KB |
Output is correct |
6 |
Correct |
80 ms |
163420 KB |
Output is correct |
7 |
Correct |
81 ms |
162708 KB |
Output is correct |
8 |
Correct |
82 ms |
162840 KB |
Output is correct |
9 |
Correct |
82 ms |
161816 KB |
Output is correct |
10 |
Correct |
81 ms |
163532 KB |
Output is correct |
11 |
Correct |
80 ms |
163524 KB |
Output is correct |
12 |
Correct |
22 ms |
44204 KB |
Output is correct |
13 |
Correct |
41 ms |
77388 KB |
Output is correct |
14 |
Correct |
55 ms |
111744 KB |
Output is correct |
15 |
Correct |
84 ms |
163528 KB |
Output is correct |
16 |
Correct |
91 ms |
163412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |