#include<bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
vector<int>a,b,c;
vector<vector<vector<vector<int>>>>dp;
int f(int x,int y,int z,int t){
if(x==a.size() && y==b.size() && z==c.size())return 0;
if(dp[x][y][z][t]!=-1)return dp[x][y][z][t];
int mn=1e15;
if(x!=a.size() && t!=1){
int ind=upper_bound(all(b),a[x])-b.begin();
ind=min(ind,y);
int ind2=upper_bound(all(c),a[x])-c.begin();
ind2=min(ind2,z);
mn=min(mn,f(x+1,y,z,1)+y-ind+z-ind2);
}
if(y!=b.size() && t!=2){
int ind=upper_bound(all(a),b[y])-a.begin();
ind=min(ind,x);
int ind2=upper_bound(all(c),b[y])-c.begin();
ind2=min(ind2,z);
mn=min(mn,f(x,y+1,z,2)+x-ind+z-ind2);
}
if(z!=c.size() && t!=3){
int ind=upper_bound(all(a),c[z])-a.begin();
ind=min(ind,x);
int ind2=upper_bound(all(b),c[z])-b.begin();
ind2=min(ind2,y);
mn=min(mn,f(x,y,z+1,3)+x-ind+y-ind2);
}
return dp[x][y][z][t]=mn;
}
int32_t main(){
fast;
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='R'){
a.pb(i);
}
if(s[i]=='G'){
b.pb(i);
}
if(s[i]=='Y'){
c.pb(i);
}
}
dp.assign((int)a.size()+5,vector<vector<vector<int>>>((int)b.size()+5,vector<vector<int>>((int)c.size()+5,vector<int>(4,-1))));
int ans=f(0,0,0,0);
if(ans==1e15)cout<<-1<<endl;
else cout<<ans<<endl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'long long int f(long long int, long long int, long long int, long long int)':
joi2019_ho_t3.cpp:16:6: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(x==a.size() && y==b.size() && z==c.size())return 0;
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:16:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(x==a.size() && y==b.size() && z==c.size())return 0;
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:16:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(x==a.size() && y==b.size() && z==c.size())return 0;
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:19:6: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if(x!=a.size() && t!=1){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:26:6: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if(y!=b.size() && t!=2){
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:33:6: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if(z!=c.size() && t!=3){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
856 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
1368 KB |
Output is correct |
29 |
Correct |
2 ms |
1368 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
31 |
Correct |
2 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1368 KB |
Output is correct |
33 |
Correct |
1 ms |
856 KB |
Output is correct |
34 |
Correct |
1 ms |
1112 KB |
Output is correct |
35 |
Correct |
2 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1368 KB |
Output is correct |
37 |
Correct |
2 ms |
1368 KB |
Output is correct |
38 |
Correct |
1 ms |
1368 KB |
Output is correct |
39 |
Correct |
2 ms |
1368 KB |
Output is correct |
40 |
Correct |
1 ms |
856 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
16472 KB |
Output is correct |
3 |
Correct |
18 ms |
16732 KB |
Output is correct |
4 |
Correct |
15 ms |
16476 KB |
Output is correct |
5 |
Correct |
14 ms |
16472 KB |
Output is correct |
6 |
Correct |
15 ms |
16640 KB |
Output is correct |
7 |
Correct |
17 ms |
16472 KB |
Output is correct |
8 |
Correct |
14 ms |
16472 KB |
Output is correct |
9 |
Correct |
14 ms |
16476 KB |
Output is correct |
10 |
Correct |
15 ms |
16472 KB |
Output is correct |
11 |
Correct |
15 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
4956 KB |
Output is correct |
13 |
Correct |
7 ms |
8024 KB |
Output is correct |
14 |
Correct |
11 ms |
11608 KB |
Output is correct |
15 |
Correct |
14 ms |
16476 KB |
Output is correct |
16 |
Correct |
14 ms |
16476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
1368 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
856 KB |
Output is correct |
26 |
Correct |
1 ms |
856 KB |
Output is correct |
27 |
Correct |
2 ms |
1368 KB |
Output is correct |
28 |
Correct |
2 ms |
1368 KB |
Output is correct |
29 |
Correct |
2 ms |
1368 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
31 |
Correct |
2 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1368 KB |
Output is correct |
33 |
Correct |
1 ms |
856 KB |
Output is correct |
34 |
Correct |
1 ms |
1112 KB |
Output is correct |
35 |
Correct |
2 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1368 KB |
Output is correct |
37 |
Correct |
2 ms |
1368 KB |
Output is correct |
38 |
Correct |
1 ms |
1368 KB |
Output is correct |
39 |
Correct |
2 ms |
1368 KB |
Output is correct |
40 |
Correct |
1 ms |
856 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
14 ms |
16472 KB |
Output is correct |
44 |
Correct |
18 ms |
16732 KB |
Output is correct |
45 |
Correct |
15 ms |
16476 KB |
Output is correct |
46 |
Correct |
14 ms |
16472 KB |
Output is correct |
47 |
Correct |
15 ms |
16640 KB |
Output is correct |
48 |
Correct |
17 ms |
16472 KB |
Output is correct |
49 |
Correct |
14 ms |
16472 KB |
Output is correct |
50 |
Correct |
14 ms |
16476 KB |
Output is correct |
51 |
Correct |
15 ms |
16472 KB |
Output is correct |
52 |
Correct |
15 ms |
16476 KB |
Output is correct |
53 |
Correct |
4 ms |
4956 KB |
Output is correct |
54 |
Correct |
7 ms |
8024 KB |
Output is correct |
55 |
Correct |
11 ms |
11608 KB |
Output is correct |
56 |
Correct |
14 ms |
16476 KB |
Output is correct |
57 |
Correct |
14 ms |
16476 KB |
Output is correct |
58 |
Execution timed out |
519 ms |
187216 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |