#include <bits/stdc++.h>
using namespace std ;
const int inf = 1e7 ;
const int MAX = 400 + 5 ;
string s ;
vector< vector< vector< vector<int> > > >dp ;
short cnt[3][MAX] , occ[3][MAX] ;
int n ;
int solve(int idx , int cnt1 , int cnt2 , int cnt3 , int prv)
{
if(idx == n)
return 0 ;
int &ret = dp[cnt1][cnt2][cnt3][prv] ;
if(ret != -1)
return ret ;
ret = inf ;
for(int i = 0 ; i <= 2 ; ++i)
{
if(i == prv && idx != 0)
continue ;
int to = -1 ;
if(i == 0)
to = occ[0][cnt1] ;
if(i == 1)
to = occ[1][cnt2] ;
if(i == 2)
to = occ[2][cnt3] ;
if(to == -1)
continue ;
int cost = max(0 , cnt[0][to] - cnt1) + max(0 , cnt[1][to] - cnt2) + max(0 , cnt[2][to] - cnt3) ;
if(i == 0)
ret = min(ret , solve(idx+1 , cnt1+1 , cnt2 , cnt3 , i) + cost) ;
if(i == 1)
ret = min(ret , solve(idx+1 , cnt1 , cnt2+1 , cnt3 , i) + cost) ;
if(i == 2)
ret = min(ret , solve(idx+1 , cnt1 , cnt2 , cnt3+1 , i) + cost) ;
}
return ret ;
}
int main()
{
memset(occ , -1 , sizeof(occ)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
cin>>s ;
int cnt1 = 0 , cnt2 = 0 , cnt3 = 0 ;
for(int i = 0 ; i < n ; ++i)
{
cnt[0][i] = cnt1 , cnt[1][i] = cnt2 , cnt[2][i] = cnt3 ;
if(s[i] == 'R')
occ[0][cnt1++] = i ;
else if(s[i] == 'G')
occ[1][cnt2++] = i ;
else
occ[2][cnt3++] = i ;
}
dp = vector<vector<vector<vector<int>>>>(cnt1+1, vector<vector<vector<int>>>(cnt2+1, vector<vector<int>>(cnt3+1, vector<int>(3,-1)))) ;
int ans = solve(0 , 0 , 0 , 0 , 0) ;
if(ans >= inf)
ans = -1 ;
//testing to see if the answer will fit in short data type
assert(ans <= 32767) ;
return cout<<ans<<"\n" , 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
896 KB |
Output is correct |
19 |
Correct |
6 ms |
896 KB |
Output is correct |
20 |
Correct |
6 ms |
896 KB |
Output is correct |
21 |
Correct |
5 ms |
896 KB |
Output is correct |
22 |
Correct |
5 ms |
768 KB |
Output is correct |
23 |
Correct |
5 ms |
896 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
768 KB |
Output is correct |
28 |
Correct |
5 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
896 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
5 ms |
896 KB |
Output is correct |
32 |
Correct |
5 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
768 KB |
Output is correct |
36 |
Correct |
5 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
5 ms |
896 KB |
Output is correct |
39 |
Correct |
5 ms |
896 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
3840 KB |
Output is correct |
3 |
Correct |
9 ms |
3840 KB |
Output is correct |
4 |
Correct |
9 ms |
3840 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
9 ms |
3840 KB |
Output is correct |
7 |
Correct |
9 ms |
3840 KB |
Output is correct |
8 |
Correct |
9 ms |
3840 KB |
Output is correct |
9 |
Correct |
9 ms |
3840 KB |
Output is correct |
10 |
Correct |
9 ms |
3840 KB |
Output is correct |
11 |
Correct |
9 ms |
3840 KB |
Output is correct |
12 |
Correct |
6 ms |
1280 KB |
Output is correct |
13 |
Correct |
7 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 KB |
Output is correct |
15 |
Correct |
9 ms |
3840 KB |
Output is correct |
16 |
Correct |
9 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
896 KB |
Output is correct |
19 |
Correct |
6 ms |
896 KB |
Output is correct |
20 |
Correct |
6 ms |
896 KB |
Output is correct |
21 |
Correct |
5 ms |
896 KB |
Output is correct |
22 |
Correct |
5 ms |
768 KB |
Output is correct |
23 |
Correct |
5 ms |
896 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
768 KB |
Output is correct |
28 |
Correct |
5 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
896 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
5 ms |
896 KB |
Output is correct |
32 |
Correct |
5 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
768 KB |
Output is correct |
36 |
Correct |
5 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
5 ms |
896 KB |
Output is correct |
39 |
Correct |
5 ms |
896 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
640 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
9 ms |
3840 KB |
Output is correct |
44 |
Correct |
9 ms |
3840 KB |
Output is correct |
45 |
Correct |
9 ms |
3840 KB |
Output is correct |
46 |
Correct |
9 ms |
3840 KB |
Output is correct |
47 |
Correct |
9 ms |
3840 KB |
Output is correct |
48 |
Correct |
9 ms |
3840 KB |
Output is correct |
49 |
Correct |
9 ms |
3840 KB |
Output is correct |
50 |
Correct |
9 ms |
3840 KB |
Output is correct |
51 |
Correct |
9 ms |
3840 KB |
Output is correct |
52 |
Correct |
9 ms |
3840 KB |
Output is correct |
53 |
Correct |
6 ms |
1280 KB |
Output is correct |
54 |
Correct |
7 ms |
2048 KB |
Output is correct |
55 |
Correct |
7 ms |
2816 KB |
Output is correct |
56 |
Correct |
9 ms |
3840 KB |
Output is correct |
57 |
Correct |
9 ms |
3840 KB |
Output is correct |
58 |
Correct |
359 ms |
133240 KB |
Output is correct |
59 |
Correct |
344 ms |
133496 KB |
Output is correct |
60 |
Correct |
354 ms |
132604 KB |
Output is correct |
61 |
Correct |
370 ms |
133932 KB |
Output is correct |
62 |
Correct |
20 ms |
12416 KB |
Output is correct |
63 |
Correct |
30 ms |
20864 KB |
Output is correct |
64 |
Correct |
105 ms |
66744 KB |
Output is correct |
65 |
Correct |
173 ms |
95736 KB |
Output is correct |
66 |
Correct |
438 ms |
131996 KB |
Output is correct |
67 |
Correct |
340 ms |
131964 KB |
Output is correct |
68 |
Correct |
357 ms |
133752 KB |
Output is correct |
69 |
Correct |
355 ms |
134904 KB |
Output is correct |
70 |
Correct |
342 ms |
134264 KB |
Output is correct |
71 |
Correct |
343 ms |
131064 KB |
Output is correct |
72 |
Correct |
49 ms |
34172 KB |
Output is correct |
73 |
Correct |
16 ms |
10624 KB |
Output is correct |