#include <bits/stdc++.h>
using namespace std;
using nagai = long long;
#define sz(x) int((x).size())
vector<int>occ[3];
const int N=404;
int dp[N][N][N][3];
#define chmi(x,y) x=min(x,y)
const int oo=0x3f3f3f3f;
int prec[3][3][N][N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<n;++i){
if(s[i]=='R')occ[0].push_back(i);
if(s[i]=='G')occ[1].push_back(i);
if(s[i]=='Y')occ[2].push_back(i);
}
// memset(dp,0x3f,sizeof dp);
int arr[3];
for(arr[0]=0;arr[0]<=occ[0].size();++arr[0])
for(arr[1]=0;arr[1]<=occ[1].size();++arr[1])
for(arr[2]=0;arr[2]<=occ[2].size();++arr[2])
for(int last=0;last<3;++last)
dp[arr[0]][arr[1]][arr[2]][last]=oo;
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
for(int k=0;k<occ[i].size();++k)
for(int l=0;l<occ[j].size();++l){
for(int kek=l;kek<occ[j].size();++kek)
if(occ[j][kek]<occ[i][k])++prec[i][j][k][l];
}
int ans=oo;
for(arr[0]=0;arr[0]<=occ[0].size();++arr[0])
for(arr[1]=0;arr[1]<=occ[1].size();++arr[1])
for(arr[2]=0;arr[2]<=occ[2].size();++arr[2])
for(int last=0;last<3;++last){
int lol=dp[arr[0]][arr[1]][arr[2]][last];
// if(lol!=oo)
// cerr<<arr[0]<<' '<<arr[1]<<' '<<arr[2]<<' '<<last<<' '<<lol<<'\n';
if(arr[0]==occ[0].size()&&arr[1]==occ[1].size()&&arr[2]==occ[2].size())
ans=min(ans,lol);
for(int nw=0;nw<3;++nw){
if(nw==last)continue;
if(arr[nw]==occ[nw].size())continue;
int cost=0;
for(int x=0;x<3;++x)
cost+=prec[nw][x][arr[nw]][arr[x]];
arr[nw]++;
chmi(dp[arr[0]][arr[1]][arr[2]][nw],lol+cost);
arr[nw]--;
}
}
if(ans==oo)ans=-1;
cout<<ans<<'\n';
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:29:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[0]=0;arr[0]<=occ[0].size();++arr[0])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:30:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[1]=0;arr[1]<=occ[1].size();++arr[1])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:31:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[2]=0;arr[2]<=occ[2].size();++arr[2])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:37:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<occ[i].size();++k)
~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int l=0;l<occ[j].size();++l){
~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:39:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int kek=l;kek<occ[j].size();++kek)
~~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[0]=0;arr[0]<=occ[0].size();++arr[0])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:44:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[1]=0;arr[1]<=occ[1].size();++arr[1])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(arr[2]=0;arr[2]<=occ[2].size();++arr[2])
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(arr[0]==occ[0].size()&&arr[1]==occ[1].size()&&arr[2]==occ[2].size())
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:50:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(arr[0]==occ[0].size()&&arr[1]==occ[1].size()&&arr[2]==occ[2].size())
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:50:61: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(arr[0]==occ[0].size()&&arr[1]==occ[1].size()&&arr[2]==occ[2].size())
~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:54:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(arr[nw]==occ[nw].size())continue;
~~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
4 ms |
2560 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
4 ms |
2048 KB |
Output is correct |
23 |
Correct |
5 ms |
2432 KB |
Output is correct |
24 |
Correct |
5 ms |
2048 KB |
Output is correct |
25 |
Correct |
6 ms |
4224 KB |
Output is correct |
26 |
Correct |
6 ms |
4452 KB |
Output is correct |
27 |
Correct |
5 ms |
3456 KB |
Output is correct |
28 |
Correct |
5 ms |
2560 KB |
Output is correct |
29 |
Correct |
5 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2560 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
4224 KB |
Output is correct |
34 |
Correct |
5 ms |
3840 KB |
Output is correct |
35 |
Correct |
5 ms |
3072 KB |
Output is correct |
36 |
Correct |
4 ms |
2176 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
149 ms |
163704 KB |
Output is correct |
3 |
Correct |
153 ms |
162796 KB |
Output is correct |
4 |
Correct |
157 ms |
163960 KB |
Output is correct |
5 |
Correct |
159 ms |
163916 KB |
Output is correct |
6 |
Correct |
151 ms |
163960 KB |
Output is correct |
7 |
Correct |
149 ms |
163128 KB |
Output is correct |
8 |
Correct |
150 ms |
163240 KB |
Output is correct |
9 |
Correct |
150 ms |
162312 KB |
Output is correct |
10 |
Correct |
173 ms |
163964 KB |
Output is correct |
11 |
Correct |
144 ms |
163980 KB |
Output is correct |
12 |
Correct |
39 ms |
44664 KB |
Output is correct |
13 |
Correct |
74 ms |
77944 KB |
Output is correct |
14 |
Correct |
98 ms |
112268 KB |
Output is correct |
15 |
Correct |
145 ms |
164048 KB |
Output is correct |
16 |
Correct |
141 ms |
163960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
768 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
4 ms |
2560 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
4 ms |
2048 KB |
Output is correct |
23 |
Correct |
5 ms |
2432 KB |
Output is correct |
24 |
Correct |
5 ms |
2048 KB |
Output is correct |
25 |
Correct |
6 ms |
4224 KB |
Output is correct |
26 |
Correct |
6 ms |
4452 KB |
Output is correct |
27 |
Correct |
5 ms |
3456 KB |
Output is correct |
28 |
Correct |
5 ms |
2560 KB |
Output is correct |
29 |
Correct |
5 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2560 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
4224 KB |
Output is correct |
34 |
Correct |
5 ms |
3840 KB |
Output is correct |
35 |
Correct |
5 ms |
3072 KB |
Output is correct |
36 |
Correct |
4 ms |
2176 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
5 ms |
2688 KB |
Output is correct |
40 |
Correct |
2 ms |
640 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
149 ms |
163704 KB |
Output is correct |
44 |
Correct |
153 ms |
162796 KB |
Output is correct |
45 |
Correct |
157 ms |
163960 KB |
Output is correct |
46 |
Correct |
159 ms |
163916 KB |
Output is correct |
47 |
Correct |
151 ms |
163960 KB |
Output is correct |
48 |
Correct |
149 ms |
163128 KB |
Output is correct |
49 |
Correct |
150 ms |
163240 KB |
Output is correct |
50 |
Correct |
150 ms |
162312 KB |
Output is correct |
51 |
Correct |
173 ms |
163964 KB |
Output is correct |
52 |
Correct |
144 ms |
163980 KB |
Output is correct |
53 |
Correct |
39 ms |
44664 KB |
Output is correct |
54 |
Correct |
74 ms |
77944 KB |
Output is correct |
55 |
Correct |
98 ms |
112268 KB |
Output is correct |
56 |
Correct |
145 ms |
164048 KB |
Output is correct |
57 |
Correct |
141 ms |
163960 KB |
Output is correct |
58 |
Correct |
271 ms |
78044 KB |
Output is correct |
59 |
Correct |
234 ms |
98296 KB |
Output is correct |
60 |
Correct |
241 ms |
89712 KB |
Output is correct |
61 |
Correct |
250 ms |
86700 KB |
Output is correct |
62 |
Correct |
161 ms |
162168 KB |
Output is correct |
63 |
Correct |
174 ms |
161760 KB |
Output is correct |
64 |
Correct |
242 ms |
152056 KB |
Output is correct |
65 |
Correct |
268 ms |
140408 KB |
Output is correct |
66 |
Correct |
271 ms |
87340 KB |
Output is correct |
67 |
Correct |
243 ms |
83936 KB |
Output is correct |
68 |
Correct |
252 ms |
90488 KB |
Output is correct |
69 |
Correct |
275 ms |
87188 KB |
Output is correct |
70 |
Correct |
270 ms |
94420 KB |
Output is correct |
71 |
Correct |
268 ms |
85732 KB |
Output is correct |
72 |
Correct |
128 ms |
43512 KB |
Output is correct |
73 |
Correct |
40 ms |
4728 KB |
Output is correct |