#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define sz(s) s.size()
#define all(s) s.begin(),s.end()
//#define int long long
using namespace std;
const int N = (1e6 + 12);
const int mod = (1e9 + 7);
const int inf = (1e9 + 7);
vector<int > vec[3];
int dp[4][250][150][150];
int n;
int go(int x,int ig, int iy,int ir){
if(ig + iy + ir == n){
return 0;
}
if(dp[x][ig][iy][ir] != -1){
return dp[x][ig][iy][ir];
}
int ind = ig + ir + iy, ans = inf;
for(int i = 0;i < 3; i++){
if(i == x){
continue;
}
if(i == 0){
if(ig == vec[0].size()){
continue;
}
int id = vec[0][ig], ad = 0;
ad -= lower_bound(vec[1].begin(), vec[1].begin() + iy, id) - vec[1].begin();
ad += iy;
ad -= lower_bound(vec[2].begin(), vec[2].begin() + ir, id) - vec[2].begin();
ad += ir;
ans = min(ans, go(i, ig + 1, iy, ir) + abs((ad + id) - ind));
}
if(i == 1){
if(iy == vec[1].size()){
continue;
}
int id = vec[1][iy], ad = 0;
ad -= lower_bound(vec[0].begin(), vec[0].begin() + ig, id) - vec[0].begin();
ad += ig;
ad -= lower_bound(vec[2].begin(), vec[2].begin() + ir, id) - vec[2].begin();
ad += ir;
ans = min(ans, go(i, ig, iy + 1, ir) + abs((ad + id) - ind));
}
if(i == 2){
if(ir == vec[2].size()){
continue;
}
int id = vec[2][ir], ad = 0;
ad -= lower_bound(vec[1].begin(), vec[1].begin() + iy, id) - vec[1].begin();
ad += iy;
ad -= lower_bound(vec[0].begin(), vec[0].begin() + ig, id) - vec[0].begin();
ad += ig;
ans = min(ans, go(i, ig , iy, ir + 1) + abs((ad + id) - ind));
}
}
dp[x][ig][iy][ir] = ans;
return ans;
}
main(){
char ch;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf(" %c", &ch);
if(ch == 'G'){
vec[0].pb(i);
}
else
if(ch == 'R'){
vec[1].pb(i);
}
else{
vec[2].pb(i);
}
}
if(vec[1].size() < vec[2].size()){
swap(vec[1], vec[2]);
}
if(vec[0].size() < vec[1].size()){
swap(vec[1], vec[0]);
}
if(vec[0].size() - 1 > vec[1].size() + vec[2].size()){
cout << -1;
return 0;
}
memset(dp, -1, sizeof(dp));
cout << go(3, 0, 0, 0);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int go(int, int, int, int)':
joi2019_ho_t3.cpp:33:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ig == vec[0].size()){
~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:44:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(iy == vec[1].size()){
~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:55:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ir == vec[2].size()){
~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: At global scope:
joi2019_ho_t3.cpp:70:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c", &ch);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
88312 KB |
Output is correct |
2 |
Correct |
53 ms |
88312 KB |
Output is correct |
3 |
Correct |
54 ms |
88444 KB |
Output is correct |
4 |
Correct |
55 ms |
88440 KB |
Output is correct |
5 |
Correct |
56 ms |
88312 KB |
Output is correct |
6 |
Correct |
52 ms |
88184 KB |
Output is correct |
7 |
Correct |
53 ms |
88312 KB |
Output is correct |
8 |
Correct |
53 ms |
88312 KB |
Output is correct |
9 |
Correct |
54 ms |
88440 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
54 ms |
88440 KB |
Output is correct |
12 |
Correct |
53 ms |
88316 KB |
Output is correct |
13 |
Correct |
5 ms |
380 KB |
Output is correct |
14 |
Correct |
52 ms |
88312 KB |
Output is correct |
15 |
Correct |
53 ms |
88312 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
88312 KB |
Output is correct |
2 |
Correct |
53 ms |
88312 KB |
Output is correct |
3 |
Correct |
54 ms |
88444 KB |
Output is correct |
4 |
Correct |
55 ms |
88440 KB |
Output is correct |
5 |
Correct |
56 ms |
88312 KB |
Output is correct |
6 |
Correct |
52 ms |
88184 KB |
Output is correct |
7 |
Correct |
53 ms |
88312 KB |
Output is correct |
8 |
Correct |
53 ms |
88312 KB |
Output is correct |
9 |
Correct |
54 ms |
88440 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
54 ms |
88440 KB |
Output is correct |
12 |
Correct |
53 ms |
88316 KB |
Output is correct |
13 |
Correct |
5 ms |
380 KB |
Output is correct |
14 |
Correct |
52 ms |
88312 KB |
Output is correct |
15 |
Correct |
53 ms |
88312 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
55 ms |
88316 KB |
Output is correct |
19 |
Correct |
55 ms |
88312 KB |
Output is correct |
20 |
Correct |
54 ms |
88312 KB |
Output is correct |
21 |
Correct |
54 ms |
88312 KB |
Output is correct |
22 |
Correct |
54 ms |
88312 KB |
Output is correct |
23 |
Correct |
56 ms |
88328 KB |
Output is correct |
24 |
Correct |
53 ms |
88312 KB |
Output is correct |
25 |
Correct |
5 ms |
256 KB |
Output is correct |
26 |
Correct |
5 ms |
256 KB |
Output is correct |
27 |
Correct |
52 ms |
88312 KB |
Output is correct |
28 |
Correct |
54 ms |
88312 KB |
Output is correct |
29 |
Correct |
55 ms |
88444 KB |
Output is correct |
30 |
Correct |
53 ms |
88312 KB |
Output is correct |
31 |
Correct |
53 ms |
88312 KB |
Output is correct |
32 |
Correct |
54 ms |
88312 KB |
Output is correct |
33 |
Correct |
5 ms |
256 KB |
Output is correct |
34 |
Correct |
5 ms |
372 KB |
Output is correct |
35 |
Correct |
54 ms |
88312 KB |
Output is correct |
36 |
Correct |
53 ms |
88312 KB |
Output is correct |
37 |
Correct |
54 ms |
88312 KB |
Output is correct |
38 |
Correct |
54 ms |
88440 KB |
Output is correct |
39 |
Correct |
55 ms |
88312 KB |
Output is correct |
40 |
Correct |
5 ms |
256 KB |
Output is correct |
41 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
88308 KB |
Output is correct |
2 |
Correct |
56 ms |
88440 KB |
Output is correct |
3 |
Correct |
53 ms |
88440 KB |
Output is correct |
4 |
Correct |
52 ms |
88440 KB |
Output is correct |
5 |
Correct |
53 ms |
88440 KB |
Output is correct |
6 |
Correct |
52 ms |
88440 KB |
Output is correct |
7 |
Correct |
52 ms |
88440 KB |
Output is correct |
8 |
Correct |
55 ms |
88444 KB |
Output is correct |
9 |
Correct |
53 ms |
88440 KB |
Output is correct |
10 |
Correct |
53 ms |
88440 KB |
Output is correct |
11 |
Correct |
53 ms |
88440 KB |
Output is correct |
12 |
Correct |
53 ms |
88440 KB |
Output is correct |
13 |
Correct |
54 ms |
88440 KB |
Output is correct |
14 |
Correct |
55 ms |
88440 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
88312 KB |
Output is correct |
2 |
Correct |
53 ms |
88312 KB |
Output is correct |
3 |
Correct |
54 ms |
88444 KB |
Output is correct |
4 |
Correct |
55 ms |
88440 KB |
Output is correct |
5 |
Correct |
56 ms |
88312 KB |
Output is correct |
6 |
Correct |
52 ms |
88184 KB |
Output is correct |
7 |
Correct |
53 ms |
88312 KB |
Output is correct |
8 |
Correct |
53 ms |
88312 KB |
Output is correct |
9 |
Correct |
54 ms |
88440 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
54 ms |
88440 KB |
Output is correct |
12 |
Correct |
53 ms |
88316 KB |
Output is correct |
13 |
Correct |
5 ms |
380 KB |
Output is correct |
14 |
Correct |
52 ms |
88312 KB |
Output is correct |
15 |
Correct |
53 ms |
88312 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
55 ms |
88316 KB |
Output is correct |
19 |
Correct |
55 ms |
88312 KB |
Output is correct |
20 |
Correct |
54 ms |
88312 KB |
Output is correct |
21 |
Correct |
54 ms |
88312 KB |
Output is correct |
22 |
Correct |
54 ms |
88312 KB |
Output is correct |
23 |
Correct |
56 ms |
88328 KB |
Output is correct |
24 |
Correct |
53 ms |
88312 KB |
Output is correct |
25 |
Correct |
5 ms |
256 KB |
Output is correct |
26 |
Correct |
5 ms |
256 KB |
Output is correct |
27 |
Correct |
52 ms |
88312 KB |
Output is correct |
28 |
Correct |
54 ms |
88312 KB |
Output is correct |
29 |
Correct |
55 ms |
88444 KB |
Output is correct |
30 |
Correct |
53 ms |
88312 KB |
Output is correct |
31 |
Correct |
53 ms |
88312 KB |
Output is correct |
32 |
Correct |
54 ms |
88312 KB |
Output is correct |
33 |
Correct |
5 ms |
256 KB |
Output is correct |
34 |
Correct |
5 ms |
372 KB |
Output is correct |
35 |
Correct |
54 ms |
88312 KB |
Output is correct |
36 |
Correct |
53 ms |
88312 KB |
Output is correct |
37 |
Correct |
54 ms |
88312 KB |
Output is correct |
38 |
Correct |
54 ms |
88440 KB |
Output is correct |
39 |
Correct |
55 ms |
88312 KB |
Output is correct |
40 |
Correct |
5 ms |
256 KB |
Output is correct |
41 |
Correct |
5 ms |
256 KB |
Output is correct |
42 |
Correct |
52 ms |
88308 KB |
Output is correct |
43 |
Correct |
56 ms |
88440 KB |
Output is correct |
44 |
Correct |
53 ms |
88440 KB |
Output is correct |
45 |
Correct |
52 ms |
88440 KB |
Output is correct |
46 |
Correct |
53 ms |
88440 KB |
Output is correct |
47 |
Correct |
52 ms |
88440 KB |
Output is correct |
48 |
Correct |
52 ms |
88440 KB |
Output is correct |
49 |
Correct |
55 ms |
88444 KB |
Output is correct |
50 |
Correct |
53 ms |
88440 KB |
Output is correct |
51 |
Correct |
53 ms |
88440 KB |
Output is correct |
52 |
Correct |
53 ms |
88440 KB |
Output is correct |
53 |
Correct |
53 ms |
88440 KB |
Output is correct |
54 |
Correct |
54 ms |
88440 KB |
Output is correct |
55 |
Correct |
55 ms |
88440 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
471 ms |
88484 KB |
Output is correct |
59 |
Correct |
475 ms |
88440 KB |
Output is correct |
60 |
Correct |
490 ms |
88568 KB |
Output is correct |
61 |
Correct |
498 ms |
88568 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
57 ms |
88440 KB |
Output is correct |
64 |
Correct |
110 ms |
88440 KB |
Output is correct |
65 |
Correct |
200 ms |
88440 KB |
Output is correct |
66 |
Correct |
301 ms |
88440 KB |
Output is correct |
67 |
Correct |
283 ms |
88440 KB |
Output is correct |
68 |
Correct |
348 ms |
88440 KB |
Output is correct |
69 |
Correct |
351 ms |
88568 KB |
Output is correct |
70 |
Correct |
392 ms |
88568 KB |
Output is correct |
71 |
Correct |
413 ms |
88440 KB |
Output is correct |
72 |
Correct |
5 ms |
256 KB |
Output is correct |
73 |
Correct |
5 ms |
376 KB |
Output is correct |