#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,fma,tune=native")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 203;
void minq(int &a, int b) {
if(a > b) a = b;
}
int dp[maxn][maxn][maxn][3];
int n;
string s;
vi p[3];
int id(char c) {
if(c=='R') return 0;
if(c=='G') return 1;
return 2;
}
int pp[maxn][maxn][3][3];
void lb(int c, int n1, int i, int j, int &ans) {
if(pp[i][j][c][n1]!= -1) {
ans -= pp[i][j][c][n1];
return;
}
int t, z;
for(t = 0, z = 1<<9; z>>=1;)
if(t+z<p[n1].size()&&p[n1][t+z]<p[c][i]) t+=z;
if(p[n1].size()&&p[n1][0]<p[c][i])
ans -= (pp[i][j][c][n1]=min(t+1, j));
else
pp[i][j][c][n1] = 0;
}
inline int cost(int i, int j, int k, int l, int w) {
if(l == w) return 1<<20;
int c, n1, n2;
if(w==0) {
c = 0;
n1 = 1;
n2 = 2;
}
if(w==1) {// i, j, k => j i k
swap(i, j);
c = 1;
n1 = 0;
n2 = 2;
}
if(w==2) {// i, j, k => k, j, i
swap(i, k);
c = 2;
n1 = 1;
n2 = 0;
}
if(p[c].size()<=i) return 1<<20;
int ans = p[c][i] - i;
lb(c, n1, i, j, ans);
lb(c, n2, i, k, ans);
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(pp, -1, sizeof pp);
cin >> n >> s;
for(int i = 0; i < n; i++) p[id(s[i])].pb(i);
if(p[0].size() > (n+1)/2) return cout << -1, 0;
if(p[1].size() > (n+1)/2) return cout << -1, 0;
if(p[2].size() > (n+1)/2) return cout << -1, 0;
memset(dp, 0x3f, sizeof dp);
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
int ans = 1<<30;
for(int i = 0; i <= p[0].size(); i++) {
for(int j = 0; j <= p[1].size(); j++) {
for(int k = 0; k <= p[2].size(); k++) {
for(int l = 0; l < 3; l++) {
if(i+j+k==n) ans = min(ans, dp[i][j][k][l]);
if(dp[i][j][k][l]>n*n) continue;
minq(dp[i+1][j][k][0], dp[i][j][k][l] + cost(i, j, k, l, 0));
minq(dp[i][j+1][k][1], dp[i][j][k][l] + cost(i, j, k, l, 1));
minq(dp[i][j][k+1][2], dp[i][j][k][l] + cost(i, j, k, l, 2));
}
}
}
}
if(ans>n*n) ans = -1;
cout << ans;
}
Compilation message
joi2019_ho_t3.cpp: In function 'void lb(int, int, int, int, int&)':
joi2019_ho_t3.cpp:32:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(t+z<p[n1].size()&&p[n1][t+z]<p[c][i]) t+=z;
~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int cost(int, int, int, int, int)':
joi2019_ho_t3.cpp:58:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p[c].size()<=i) return 1<<20;
~~~~~~~~~~~^~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:70:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p[0].size() > (n+1)/2) return cout << -1, 0;
~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:71:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p[1].size() > (n+1)/2) return cout << -1, 0;
~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:72:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p[2].size() > (n+1)/2) return cout << -1, 0;
~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:76:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i <= p[0].size(); i++) {
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:77:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j <= p[1].size(); j++) {
~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:78:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0; k <= p[2].size(); k++) {
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
100088 KB |
Output is correct |
2 |
Correct |
58 ms |
99960 KB |
Output is correct |
3 |
Correct |
83 ms |
99960 KB |
Output is correct |
4 |
Correct |
59 ms |
99960 KB |
Output is correct |
5 |
Correct |
60 ms |
100088 KB |
Output is correct |
6 |
Correct |
61 ms |
100052 KB |
Output is correct |
7 |
Correct |
68 ms |
100024 KB |
Output is correct |
8 |
Correct |
60 ms |
100064 KB |
Output is correct |
9 |
Correct |
60 ms |
99960 KB |
Output is correct |
10 |
Correct |
7 ms |
1788 KB |
Output is correct |
11 |
Correct |
59 ms |
100088 KB |
Output is correct |
12 |
Correct |
75 ms |
100088 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
63 ms |
99940 KB |
Output is correct |
15 |
Correct |
66 ms |
99960 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
6 ms |
1760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
100088 KB |
Output is correct |
2 |
Correct |
58 ms |
99960 KB |
Output is correct |
3 |
Correct |
83 ms |
99960 KB |
Output is correct |
4 |
Correct |
59 ms |
99960 KB |
Output is correct |
5 |
Correct |
60 ms |
100088 KB |
Output is correct |
6 |
Correct |
61 ms |
100052 KB |
Output is correct |
7 |
Correct |
68 ms |
100024 KB |
Output is correct |
8 |
Correct |
60 ms |
100064 KB |
Output is correct |
9 |
Correct |
60 ms |
99960 KB |
Output is correct |
10 |
Correct |
7 ms |
1788 KB |
Output is correct |
11 |
Correct |
59 ms |
100088 KB |
Output is correct |
12 |
Correct |
75 ms |
100088 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
63 ms |
99940 KB |
Output is correct |
15 |
Correct |
66 ms |
99960 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
6 ms |
1760 KB |
Output is correct |
18 |
Correct |
62 ms |
100004 KB |
Output is correct |
19 |
Correct |
60 ms |
100088 KB |
Output is correct |
20 |
Correct |
63 ms |
100008 KB |
Output is correct |
21 |
Correct |
69 ms |
99992 KB |
Output is correct |
22 |
Correct |
63 ms |
100068 KB |
Output is correct |
23 |
Correct |
64 ms |
100064 KB |
Output is correct |
24 |
Correct |
64 ms |
100064 KB |
Output is correct |
25 |
Correct |
6 ms |
1784 KB |
Output is correct |
26 |
Correct |
6 ms |
1784 KB |
Output is correct |
27 |
Correct |
61 ms |
99960 KB |
Output is correct |
28 |
Correct |
63 ms |
99972 KB |
Output is correct |
29 |
Correct |
73 ms |
100012 KB |
Output is correct |
30 |
Correct |
64 ms |
99960 KB |
Output is correct |
31 |
Correct |
67 ms |
99960 KB |
Output is correct |
32 |
Correct |
61 ms |
100064 KB |
Output is correct |
33 |
Correct |
6 ms |
1784 KB |
Output is correct |
34 |
Correct |
6 ms |
1784 KB |
Output is correct |
35 |
Correct |
62 ms |
100064 KB |
Output is correct |
36 |
Correct |
64 ms |
100064 KB |
Output is correct |
37 |
Correct |
64 ms |
99960 KB |
Output is correct |
38 |
Correct |
63 ms |
99960 KB |
Output is correct |
39 |
Correct |
58 ms |
99944 KB |
Output is correct |
40 |
Correct |
6 ms |
1784 KB |
Output is correct |
41 |
Correct |
7 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
100040 KB |
Output is correct |
2 |
Correct |
60 ms |
100120 KB |
Output is correct |
3 |
Correct |
63 ms |
100008 KB |
Output is correct |
4 |
Correct |
62 ms |
100216 KB |
Output is correct |
5 |
Correct |
62 ms |
100016 KB |
Output is correct |
6 |
Correct |
61 ms |
100088 KB |
Output is correct |
7 |
Correct |
65 ms |
99964 KB |
Output is correct |
8 |
Correct |
60 ms |
99960 KB |
Output is correct |
9 |
Correct |
64 ms |
99960 KB |
Output is correct |
10 |
Correct |
66 ms |
100024 KB |
Output is correct |
11 |
Correct |
60 ms |
100012 KB |
Output is correct |
12 |
Correct |
63 ms |
99960 KB |
Output is correct |
13 |
Correct |
60 ms |
99960 KB |
Output is correct |
14 |
Correct |
68 ms |
99960 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
5 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
100088 KB |
Output is correct |
2 |
Correct |
58 ms |
99960 KB |
Output is correct |
3 |
Correct |
83 ms |
99960 KB |
Output is correct |
4 |
Correct |
59 ms |
99960 KB |
Output is correct |
5 |
Correct |
60 ms |
100088 KB |
Output is correct |
6 |
Correct |
61 ms |
100052 KB |
Output is correct |
7 |
Correct |
68 ms |
100024 KB |
Output is correct |
8 |
Correct |
60 ms |
100064 KB |
Output is correct |
9 |
Correct |
60 ms |
99960 KB |
Output is correct |
10 |
Correct |
7 ms |
1788 KB |
Output is correct |
11 |
Correct |
59 ms |
100088 KB |
Output is correct |
12 |
Correct |
75 ms |
100088 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
63 ms |
99940 KB |
Output is correct |
15 |
Correct |
66 ms |
99960 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
6 ms |
1760 KB |
Output is correct |
18 |
Correct |
62 ms |
100004 KB |
Output is correct |
19 |
Correct |
60 ms |
100088 KB |
Output is correct |
20 |
Correct |
63 ms |
100008 KB |
Output is correct |
21 |
Correct |
69 ms |
99992 KB |
Output is correct |
22 |
Correct |
63 ms |
100068 KB |
Output is correct |
23 |
Correct |
64 ms |
100064 KB |
Output is correct |
24 |
Correct |
64 ms |
100064 KB |
Output is correct |
25 |
Correct |
6 ms |
1784 KB |
Output is correct |
26 |
Correct |
6 ms |
1784 KB |
Output is correct |
27 |
Correct |
61 ms |
99960 KB |
Output is correct |
28 |
Correct |
63 ms |
99972 KB |
Output is correct |
29 |
Correct |
73 ms |
100012 KB |
Output is correct |
30 |
Correct |
64 ms |
99960 KB |
Output is correct |
31 |
Correct |
67 ms |
99960 KB |
Output is correct |
32 |
Correct |
61 ms |
100064 KB |
Output is correct |
33 |
Correct |
6 ms |
1784 KB |
Output is correct |
34 |
Correct |
6 ms |
1784 KB |
Output is correct |
35 |
Correct |
62 ms |
100064 KB |
Output is correct |
36 |
Correct |
64 ms |
100064 KB |
Output is correct |
37 |
Correct |
64 ms |
99960 KB |
Output is correct |
38 |
Correct |
63 ms |
99960 KB |
Output is correct |
39 |
Correct |
58 ms |
99944 KB |
Output is correct |
40 |
Correct |
6 ms |
1784 KB |
Output is correct |
41 |
Correct |
7 ms |
1784 KB |
Output is correct |
42 |
Correct |
59 ms |
100040 KB |
Output is correct |
43 |
Correct |
60 ms |
100120 KB |
Output is correct |
44 |
Correct |
63 ms |
100008 KB |
Output is correct |
45 |
Correct |
62 ms |
100216 KB |
Output is correct |
46 |
Correct |
62 ms |
100016 KB |
Output is correct |
47 |
Correct |
61 ms |
100088 KB |
Output is correct |
48 |
Correct |
65 ms |
99964 KB |
Output is correct |
49 |
Correct |
60 ms |
99960 KB |
Output is correct |
50 |
Correct |
64 ms |
99960 KB |
Output is correct |
51 |
Correct |
66 ms |
100024 KB |
Output is correct |
52 |
Correct |
60 ms |
100012 KB |
Output is correct |
53 |
Correct |
63 ms |
99960 KB |
Output is correct |
54 |
Correct |
60 ms |
99960 KB |
Output is correct |
55 |
Correct |
68 ms |
99960 KB |
Output is correct |
56 |
Correct |
6 ms |
1784 KB |
Output is correct |
57 |
Correct |
5 ms |
1784 KB |
Output is correct |
58 |
Correct |
135 ms |
100028 KB |
Output is correct |
59 |
Correct |
134 ms |
100064 KB |
Output is correct |
60 |
Correct |
134 ms |
99960 KB |
Output is correct |
61 |
Correct |
139 ms |
99964 KB |
Output is correct |
62 |
Correct |
5 ms |
1784 KB |
Output is correct |
63 |
Correct |
67 ms |
99960 KB |
Output is correct |
64 |
Correct |
88 ms |
99960 KB |
Output is correct |
65 |
Correct |
99 ms |
100064 KB |
Output is correct |
66 |
Correct |
134 ms |
99960 KB |
Output is correct |
67 |
Correct |
134 ms |
99960 KB |
Output is correct |
68 |
Correct |
132 ms |
99960 KB |
Output is correct |
69 |
Correct |
141 ms |
100088 KB |
Output is correct |
70 |
Correct |
146 ms |
99960 KB |
Output is correct |
71 |
Correct |
133 ms |
100088 KB |
Output is correct |
72 |
Correct |
6 ms |
1784 KB |
Output is correct |
73 |
Correct |
6 ms |
1784 KB |
Output is correct |