//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][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][1][cnt[0]]=cnt[1],pos[0][2][cnt[0]]=cnt[2];
else if (s[i]=='G') cnt[1]++,a[i]=1,pos[1][0][cnt[1]]=cnt[0],pos[1][2][cnt[1]]=cnt[2];
else cnt[2]++,a[i]=2,pos[2][0][cnt[2]]=cnt[0],pos[2][1][cnt[2]]=cnt[1];
}
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])+_-i-min(j,pos[0][1][i+1])-min(k,pos[0][2][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])+_-j-min(i,pos[1][0][j+1])-min(k,pos[1][2][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])+_-k-min(i,pos[2][0][k+1])-min(j,pos[2][1][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;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
1 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
1 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3144 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Correct |
1 ms |
2248 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
77 ms |
163420 KB |
Output is correct |
3 |
Correct |
83 ms |
162628 KB |
Output is correct |
4 |
Correct |
78 ms |
163512 KB |
Output is correct |
5 |
Correct |
76 ms |
163468 KB |
Output is correct |
6 |
Correct |
78 ms |
163532 KB |
Output is correct |
7 |
Correct |
79 ms |
162680 KB |
Output is correct |
8 |
Correct |
79 ms |
162716 KB |
Output is correct |
9 |
Correct |
83 ms |
161880 KB |
Output is correct |
10 |
Correct |
79 ms |
163432 KB |
Output is correct |
11 |
Correct |
80 ms |
163532 KB |
Output is correct |
12 |
Correct |
21 ms |
44164 KB |
Output is correct |
13 |
Correct |
38 ms |
77404 KB |
Output is correct |
14 |
Correct |
53 ms |
111692 KB |
Output is correct |
15 |
Correct |
82 ms |
163528 KB |
Output is correct |
16 |
Correct |
86 ms |
163440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
1 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
1 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3144 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
2 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Correct |
1 ms |
2248 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
77 ms |
163420 KB |
Output is correct |
44 |
Correct |
83 ms |
162628 KB |
Output is correct |
45 |
Correct |
78 ms |
163512 KB |
Output is correct |
46 |
Correct |
76 ms |
163468 KB |
Output is correct |
47 |
Correct |
78 ms |
163532 KB |
Output is correct |
48 |
Correct |
79 ms |
162680 KB |
Output is correct |
49 |
Correct |
79 ms |
162716 KB |
Output is correct |
50 |
Correct |
83 ms |
161880 KB |
Output is correct |
51 |
Correct |
79 ms |
163432 KB |
Output is correct |
52 |
Correct |
80 ms |
163532 KB |
Output is correct |
53 |
Correct |
21 ms |
44164 KB |
Output is correct |
54 |
Correct |
38 ms |
77404 KB |
Output is correct |
55 |
Correct |
53 ms |
111692 KB |
Output is correct |
56 |
Correct |
82 ms |
163528 KB |
Output is correct |
57 |
Correct |
86 ms |
163440 KB |
Output is correct |
58 |
Correct |
134 ms |
120716 KB |
Output is correct |
59 |
Correct |
139 ms |
137996 KB |
Output is correct |
60 |
Correct |
144 ms |
129948 KB |
Output is correct |
61 |
Correct |
128 ms |
127708 KB |
Output is correct |
62 |
Correct |
85 ms |
163076 KB |
Output is correct |
63 |
Correct |
90 ms |
164444 KB |
Output is correct |
64 |
Correct |
112 ms |
164556 KB |
Output is correct |
65 |
Correct |
155 ms |
158776 KB |
Output is correct |
66 |
Correct |
124 ms |
127996 KB |
Output is correct |
67 |
Correct |
131 ms |
125348 KB |
Output is correct |
68 |
Correct |
127 ms |
131276 KB |
Output is correct |
69 |
Correct |
130 ms |
128820 KB |
Output is correct |
70 |
Correct |
130 ms |
134572 KB |
Output is correct |
71 |
Correct |
130 ms |
126372 KB |
Output is correct |
72 |
Correct |
43 ms |
49492 KB |
Output is correct |
73 |
Correct |
5 ms |
5832 KB |
Output is correct |