#include <bits/stdc++.h>
using namespace std;
const int MN = 410;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> va;
typedef vector<va> vb;
typedef vector<vb> vc;
const int INF = 1e9;
struct P {
int pr,pg,py;
};
vc dp;
vi kt[3];
vector<P> po[3];
int main() {
int n;
cin >> n;
string s;
cin >> s;
for(int i=0;i<s.size();i++) {
P pos{int(kt[0].size()),int(kt[1].size()),int(kt[2].size())};
if(s[i] == 'R') {
kt[0].push_back(i);
po[0].push_back(pos);
} else if(s[i] == 'G') {
kt[1].push_back(i);
po[1].push_back(pos);
} else {
kt[2].push_back(i);
po[2].push_back(pos);
}
}
dp.assign(3,vb(kt[0].size()+1,va(kt[1].size()+1,vi(kt[2].size()+1,INF))));
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
for(int i=0;i<=kt[0].size();i++) {
for(int j=0;j<=kt[1].size();j++) {
for(int k=0;k<=kt[2].size();k++) {
for(int c=0;c<3;c++) {
int cur = dp[c][i][j][k];
if(c != 0 && i < kt[0].size()) {
P pos = po[0][i];
int co = max(0,pos.pg-j) + max(0,pos.py-k);
int& nx = dp[0][i+1][j][k];
nx = min(nx,cur+co);
}
if(c != 1 && j < kt[1].size()) {
P pos = po[1][j];
int co = max(0,pos.pr-i) + max(0,pos.py-k);
int& nx = dp[1][i][j+1][k];
nx = min(nx,cur+co);
}
if(c != 2 && k < kt[2].size()) {
P pos = po[2][k];
int co = max(0,pos.pr-i) + max(0,pos.pg-j);
int& nx = dp[2][i][j][k+1];
nx = min(nx,cur+co);
}
}
}
}
}
int res = INF;
for(int i=0;i<3;i++) {
res = min(res,dp[i][kt[0].size()][kt[1].size()][kt[2].size()]);
}
if(res == INF) {
cout << -1 << '\n';
} else {
cout << res << '\n';
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:21:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0;i<s.size();i++) {
| ~^~~~~~~~~
joi2019_ho_t3.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i=0;i<=kt[0].size();i++) {
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j=0;j<=kt[1].size();j++) {
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int k=0;k<=kt[2].size();k++) {
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:41:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(c != 0 && i < kt[0].size()) {
| ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:47:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(c != 1 && j < kt[1].size()) {
| ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | if(c != 2 && k < kt[2].size()) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
16 ms |
9216 KB |
Output is correct |
3 |
Correct |
18 ms |
9216 KB |
Output is correct |
4 |
Correct |
17 ms |
9216 KB |
Output is correct |
5 |
Correct |
34 ms |
9208 KB |
Output is correct |
6 |
Correct |
14 ms |
9216 KB |
Output is correct |
7 |
Correct |
26 ms |
9216 KB |
Output is correct |
8 |
Correct |
14 ms |
9216 KB |
Output is correct |
9 |
Correct |
13 ms |
9088 KB |
Output is correct |
10 |
Correct |
14 ms |
9216 KB |
Output is correct |
11 |
Correct |
15 ms |
9216 KB |
Output is correct |
12 |
Correct |
4 ms |
2688 KB |
Output is correct |
13 |
Correct |
10 ms |
4480 KB |
Output is correct |
14 |
Correct |
10 ms |
6400 KB |
Output is correct |
15 |
Correct |
18 ms |
9216 KB |
Output is correct |
16 |
Correct |
14 ms |
9216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
16 ms |
9216 KB |
Output is correct |
44 |
Correct |
18 ms |
9216 KB |
Output is correct |
45 |
Correct |
17 ms |
9216 KB |
Output is correct |
46 |
Correct |
34 ms |
9208 KB |
Output is correct |
47 |
Correct |
14 ms |
9216 KB |
Output is correct |
48 |
Correct |
26 ms |
9216 KB |
Output is correct |
49 |
Correct |
14 ms |
9216 KB |
Output is correct |
50 |
Correct |
13 ms |
9088 KB |
Output is correct |
51 |
Correct |
14 ms |
9216 KB |
Output is correct |
52 |
Correct |
15 ms |
9216 KB |
Output is correct |
53 |
Correct |
4 ms |
2688 KB |
Output is correct |
54 |
Correct |
10 ms |
4480 KB |
Output is correct |
55 |
Correct |
10 ms |
6400 KB |
Output is correct |
56 |
Correct |
18 ms |
9216 KB |
Output is correct |
57 |
Correct |
14 ms |
9216 KB |
Output is correct |
58 |
Correct |
123 ms |
40696 KB |
Output is correct |
59 |
Correct |
95 ms |
41472 KB |
Output is correct |
60 |
Correct |
116 ms |
39936 KB |
Output is correct |
61 |
Correct |
124 ms |
40832 KB |
Output is correct |
62 |
Correct |
23 ms |
9032 KB |
Output is correct |
63 |
Correct |
27 ms |
11264 KB |
Output is correct |
64 |
Correct |
67 ms |
24736 KB |
Output is correct |
65 |
Correct |
91 ms |
30840 KB |
Output is correct |
66 |
Correct |
133 ms |
40364 KB |
Output is correct |
67 |
Correct |
128 ms |
40056 KB |
Output is correct |
68 |
Correct |
121 ms |
40380 KB |
Output is correct |
69 |
Correct |
129 ms |
41088 KB |
Output is correct |
70 |
Correct |
121 ms |
40952 KB |
Output is correct |
71 |
Correct |
119 ms |
40320 KB |
Output is correct |
72 |
Correct |
30 ms |
11264 KB |
Output is correct |
73 |
Correct |
10 ms |
2944 KB |
Output is correct |