//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
const int MAX_N = 402;
const int INF = 1e9;
int n, f[MAX_N][MAX_N][MAX_N][5];
bool calculated[MAX_N][MAX_N][MAX_N][5];
vector<int> a[3];
void readInput() {
cin >> n;
for (int i=1; i<=n; ++i) {
char c;
cin >> c;
if (c=='R')
a[0].push_back(i);
else if (c=='G')
a[1].push_back(i);
else
a[2].push_back(i);
}
}
int bisect(int idx, int curPos, int bound) {
int l = 0, r = bound;
for (int mid=(l+r)/2; mid!=l && r!=mid; mid=(l+r)/2) {
if (a[idx][mid]>curPos)
r = mid;
else
l = mid;
}
for (int i=l; i<=r; ++i) {
if (a[idx][i]>curPos)
return bound - i + 1;
}
return 0;
}
int cost(int curPos, int newPos, int i, int j, int k) {
int realPos = curPos + bisect(0, curPos, i-1)
+ bisect(1, curPos, j-1) + bisect(2, curPos, k-1);
return realPos - newPos;
}
int dp(int cur, int i, int j, int k, int prv) {
if (cur==n+1)
return 0;
if (calculated[i][j][k][prv])
return f[i][j][k][prv];
calculated[i][j][k][prv] = true;
int res = INF;
if (i<a[0].size() && prv!=0) {
res = min(res, dp(cur+1, i+1, j, k, 0) +
cost(a[0][i], cur, i, j, k));
}
if (j<a[1].size() && prv!=1) {
res = min(res, dp(cur+1, i, j+1, k, 1) +
cost(a[1][j], cur, i, j, k));
}
if (k!=a[2].size() && prv!=2) {
res = min(res, dp(cur+1, i, j, k+1, 2) +
cost(a[2][k], cur, i, j, k));
}
return f[i][j][k][prv] = res;
}
void solve() {
int tmp = dp(1, 0, 0, 0, 3);
if (tmp==INF)
cout << -1;
else
cout << tmp;
}
int main() {
#ifdef GLASSES_GIRL
freopen(FILE_NAME".in", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
readInput();
solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'int dp(int, int, int, int, int)':
joi2019_ho_t3.cpp:70:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i<a[0].size() && prv!=0) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:74:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j<a[1].size() && prv!=1) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:78:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k!=a[2].size() && prv!=2) {
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
768 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
768 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
3328 KB |
Output is correct |
19 |
Correct |
7 ms |
2816 KB |
Output is correct |
20 |
Correct |
8 ms |
3200 KB |
Output is correct |
21 |
Correct |
7 ms |
3456 KB |
Output is correct |
22 |
Correct |
5 ms |
2432 KB |
Output is correct |
23 |
Correct |
8 ms |
3072 KB |
Output is correct |
24 |
Correct |
7 ms |
2176 KB |
Output is correct |
25 |
Correct |
3 ms |
1152 KB |
Output is correct |
26 |
Correct |
3 ms |
1408 KB |
Output is correct |
27 |
Correct |
7 ms |
3456 KB |
Output is correct |
28 |
Correct |
8 ms |
3328 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
3200 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
8 ms |
3328 KB |
Output is correct |
33 |
Correct |
4 ms |
1664 KB |
Output is correct |
34 |
Correct |
4 ms |
1920 KB |
Output is correct |
35 |
Correct |
7 ms |
3456 KB |
Output is correct |
36 |
Correct |
6 ms |
2688 KB |
Output is correct |
37 |
Correct |
6 ms |
2432 KB |
Output is correct |
38 |
Correct |
6 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
3328 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
5504 KB |
Output is correct |
3 |
Correct |
4 ms |
5504 KB |
Output is correct |
4 |
Correct |
7 ms |
5504 KB |
Output is correct |
5 |
Correct |
7 ms |
5504 KB |
Output is correct |
6 |
Correct |
7 ms |
5504 KB |
Output is correct |
7 |
Correct |
18 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5504 KB |
Output is correct |
9 |
Correct |
7 ms |
5504 KB |
Output is correct |
10 |
Correct |
6 ms |
5504 KB |
Output is correct |
11 |
Correct |
6 ms |
5504 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
6 ms |
3840 KB |
Output is correct |
14 |
Correct |
7 ms |
4608 KB |
Output is correct |
15 |
Correct |
6 ms |
5504 KB |
Output is correct |
16 |
Correct |
7 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
768 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
3328 KB |
Output is correct |
19 |
Correct |
7 ms |
2816 KB |
Output is correct |
20 |
Correct |
8 ms |
3200 KB |
Output is correct |
21 |
Correct |
7 ms |
3456 KB |
Output is correct |
22 |
Correct |
5 ms |
2432 KB |
Output is correct |
23 |
Correct |
8 ms |
3072 KB |
Output is correct |
24 |
Correct |
7 ms |
2176 KB |
Output is correct |
25 |
Correct |
3 ms |
1152 KB |
Output is correct |
26 |
Correct |
3 ms |
1408 KB |
Output is correct |
27 |
Correct |
7 ms |
3456 KB |
Output is correct |
28 |
Correct |
8 ms |
3328 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
3200 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
8 ms |
3328 KB |
Output is correct |
33 |
Correct |
4 ms |
1664 KB |
Output is correct |
34 |
Correct |
4 ms |
1920 KB |
Output is correct |
35 |
Correct |
7 ms |
3456 KB |
Output is correct |
36 |
Correct |
6 ms |
2688 KB |
Output is correct |
37 |
Correct |
6 ms |
2432 KB |
Output is correct |
38 |
Correct |
6 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
3328 KB |
Output is correct |
40 |
Correct |
3 ms |
512 KB |
Output is correct |
41 |
Correct |
4 ms |
1152 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
5504 KB |
Output is correct |
44 |
Correct |
4 ms |
5504 KB |
Output is correct |
45 |
Correct |
7 ms |
5504 KB |
Output is correct |
46 |
Correct |
7 ms |
5504 KB |
Output is correct |
47 |
Correct |
7 ms |
5504 KB |
Output is correct |
48 |
Correct |
18 ms |
5504 KB |
Output is correct |
49 |
Correct |
7 ms |
5504 KB |
Output is correct |
50 |
Correct |
7 ms |
5504 KB |
Output is correct |
51 |
Correct |
6 ms |
5504 KB |
Output is correct |
52 |
Correct |
6 ms |
5504 KB |
Output is correct |
53 |
Correct |
5 ms |
2944 KB |
Output is correct |
54 |
Correct |
6 ms |
3840 KB |
Output is correct |
55 |
Correct |
7 ms |
4608 KB |
Output is correct |
56 |
Correct |
6 ms |
5504 KB |
Output is correct |
57 |
Correct |
7 ms |
5504 KB |
Output is correct |
58 |
Execution timed out |
1053 ms |
105160 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |