#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
#define nn "\n"
#define x_x ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define intt int _; cin >> _; while (_--)
#define emp push_back
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ld long double
#define A first
#define B second
typedef long long ll;
const ld eps = 1e-27;
// diff between decimals 0.000000001 mt19937 mt(time(nullptr));
int dp[205][205][205][4], ar[405]; int n; string s;
int ans(int y, int r, int g) {
int i=r+g+y;
if(i>=n) return 0;
int k=3; if(i) k=ar[i-1];
if(i&&~dp[y][r][g][k]) return dp[y][r][g][k];
int fans=2000000;
for(int z=0; z<3; z++) {
if(z==k) continue;
int j=i;
while(j<n&&ar[j]!=z) ++j;
if (j<n) {
int nm=0;
if(z==1)r++; else if(z==2)g++; else y++;
for (int x=j; x>i; x-=1) swap(ar[x], ar[x-1]), ++nm;
fans=min(fans,nm+ans(y,r,g));
for(int x=i; x<j; x++)swap(ar[x], ar[x+1]);
if(z==1)r-=1; else if(z==2)g-=1; else y-=1;
}
}
return dp[y][r][g][k]=fans;
}
int main() {
x_x
cin>>n>>s; int a=0,b=0,c=0;
for(int i=0; i<n; i++) {
if(s[i]=='R')a++, ar[i]=1; else if(s[i]=='G')b++, ar[i]=2;
else ar[i]=0, c++;
}
a=max({a,b,c}), b=n-a; --a;
if(a>b) return cout<<-1, 0;
memset(dp,-1,sizeof(dp)); a=ans(0,0,0);
cout<<(a>=2000000?-1:a);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
135064 KB |
Output is correct |
2 |
Correct |
30 ms |
135256 KB |
Output is correct |
3 |
Correct |
28 ms |
135260 KB |
Output is correct |
4 |
Correct |
28 ms |
135300 KB |
Output is correct |
5 |
Correct |
29 ms |
135256 KB |
Output is correct |
6 |
Correct |
28 ms |
135260 KB |
Output is correct |
7 |
Correct |
30 ms |
135252 KB |
Output is correct |
8 |
Correct |
28 ms |
135204 KB |
Output is correct |
9 |
Correct |
28 ms |
135256 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
28 ms |
135328 KB |
Output is correct |
12 |
Correct |
28 ms |
135248 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
28 ms |
135260 KB |
Output is correct |
15 |
Correct |
28 ms |
135252 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
135064 KB |
Output is correct |
2 |
Correct |
30 ms |
135256 KB |
Output is correct |
3 |
Correct |
28 ms |
135260 KB |
Output is correct |
4 |
Correct |
28 ms |
135300 KB |
Output is correct |
5 |
Correct |
29 ms |
135256 KB |
Output is correct |
6 |
Correct |
28 ms |
135260 KB |
Output is correct |
7 |
Correct |
30 ms |
135252 KB |
Output is correct |
8 |
Correct |
28 ms |
135204 KB |
Output is correct |
9 |
Correct |
28 ms |
135256 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
28 ms |
135328 KB |
Output is correct |
12 |
Correct |
28 ms |
135248 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
28 ms |
135260 KB |
Output is correct |
15 |
Correct |
28 ms |
135252 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
29 ms |
135260 KB |
Output is correct |
19 |
Correct |
29 ms |
135260 KB |
Output is correct |
20 |
Correct |
29 ms |
135248 KB |
Output is correct |
21 |
Correct |
29 ms |
135256 KB |
Output is correct |
22 |
Correct |
30 ms |
135260 KB |
Output is correct |
23 |
Correct |
30 ms |
135080 KB |
Output is correct |
24 |
Correct |
29 ms |
135260 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
135312 KB |
Output is correct |
28 |
Correct |
33 ms |
135248 KB |
Output is correct |
29 |
Correct |
31 ms |
135364 KB |
Output is correct |
30 |
Correct |
30 ms |
135248 KB |
Output is correct |
31 |
Correct |
29 ms |
135192 KB |
Output is correct |
32 |
Correct |
30 ms |
135256 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
28 ms |
135260 KB |
Output is correct |
36 |
Correct |
33 ms |
135292 KB |
Output is correct |
37 |
Correct |
29 ms |
135244 KB |
Output is correct |
38 |
Correct |
30 ms |
135248 KB |
Output is correct |
39 |
Correct |
30 ms |
135248 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
135296 KB |
Output is correct |
2 |
Correct |
28 ms |
135252 KB |
Output is correct |
3 |
Correct |
32 ms |
135240 KB |
Output is correct |
4 |
Correct |
29 ms |
135260 KB |
Output is correct |
5 |
Correct |
28 ms |
135260 KB |
Output is correct |
6 |
Correct |
30 ms |
135248 KB |
Output is correct |
7 |
Correct |
36 ms |
135252 KB |
Output is correct |
8 |
Correct |
31 ms |
135136 KB |
Output is correct |
9 |
Correct |
29 ms |
135500 KB |
Output is correct |
10 |
Correct |
29 ms |
135116 KB |
Output is correct |
11 |
Correct |
28 ms |
135248 KB |
Output is correct |
12 |
Correct |
29 ms |
135252 KB |
Output is correct |
13 |
Correct |
28 ms |
135256 KB |
Output is correct |
14 |
Correct |
28 ms |
135260 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
135064 KB |
Output is correct |
2 |
Correct |
30 ms |
135256 KB |
Output is correct |
3 |
Correct |
28 ms |
135260 KB |
Output is correct |
4 |
Correct |
28 ms |
135300 KB |
Output is correct |
5 |
Correct |
29 ms |
135256 KB |
Output is correct |
6 |
Correct |
28 ms |
135260 KB |
Output is correct |
7 |
Correct |
30 ms |
135252 KB |
Output is correct |
8 |
Correct |
28 ms |
135204 KB |
Output is correct |
9 |
Correct |
28 ms |
135256 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
28 ms |
135328 KB |
Output is correct |
12 |
Correct |
28 ms |
135248 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
28 ms |
135260 KB |
Output is correct |
15 |
Correct |
28 ms |
135252 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
29 ms |
135260 KB |
Output is correct |
19 |
Correct |
29 ms |
135260 KB |
Output is correct |
20 |
Correct |
29 ms |
135248 KB |
Output is correct |
21 |
Correct |
29 ms |
135256 KB |
Output is correct |
22 |
Correct |
30 ms |
135260 KB |
Output is correct |
23 |
Correct |
30 ms |
135080 KB |
Output is correct |
24 |
Correct |
29 ms |
135260 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
135312 KB |
Output is correct |
28 |
Correct |
33 ms |
135248 KB |
Output is correct |
29 |
Correct |
31 ms |
135364 KB |
Output is correct |
30 |
Correct |
30 ms |
135248 KB |
Output is correct |
31 |
Correct |
29 ms |
135192 KB |
Output is correct |
32 |
Correct |
30 ms |
135256 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
28 ms |
135260 KB |
Output is correct |
36 |
Correct |
33 ms |
135292 KB |
Output is correct |
37 |
Correct |
29 ms |
135244 KB |
Output is correct |
38 |
Correct |
30 ms |
135248 KB |
Output is correct |
39 |
Correct |
30 ms |
135248 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
28 ms |
135296 KB |
Output is correct |
43 |
Correct |
28 ms |
135252 KB |
Output is correct |
44 |
Correct |
32 ms |
135240 KB |
Output is correct |
45 |
Correct |
29 ms |
135260 KB |
Output is correct |
46 |
Correct |
28 ms |
135260 KB |
Output is correct |
47 |
Correct |
30 ms |
135248 KB |
Output is correct |
48 |
Correct |
36 ms |
135252 KB |
Output is correct |
49 |
Correct |
31 ms |
135136 KB |
Output is correct |
50 |
Correct |
29 ms |
135500 KB |
Output is correct |
51 |
Correct |
29 ms |
135116 KB |
Output is correct |
52 |
Correct |
28 ms |
135248 KB |
Output is correct |
53 |
Correct |
29 ms |
135252 KB |
Output is correct |
54 |
Correct |
28 ms |
135256 KB |
Output is correct |
55 |
Correct |
28 ms |
135260 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Execution timed out |
604 ms |
135352 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |