//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;
map<tuple<int, int, int, int>, int> f;
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 (f.count(make_tuple(i, j, k, prv)))
return f[make_tuple(i, j, k, prv)];
int res = INF;
// if (i<a[0].size())
// debug(cost(a[0][i], cur, i, j, k));
if (i<a[0].size() && prv!=0) {
// debug(cost(a[0][i], cur, i, j, k));
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) {
// debug(cost(a[1][j], cur, i, j, k));
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[make_tuple(i, j, k, prv)] = res;
}
void solve() {
//debug(dp(6, 3, 2, 0, 0));
// debug(dp(5, 2, 2, 0, 1));
// debug(cost(4, 5, 2, 2, 0));
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();
// debug(cost(5, 2, 1, 0, 0));
solve();
}
Compilation message
joi2019_ho_t3.cpp: In function 'int dp(int, int, int, int, int)':
joi2019_ho_t3.cpp:71:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i<a[0].size() && prv!=0) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:76:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j<a[1].size() && prv!=1) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:81:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k!=a[2].size() && prv!=2) {
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
12 ms |
1280 KB |
Output is correct |
19 |
Correct |
12 ms |
1152 KB |
Output is correct |
20 |
Correct |
12 ms |
1152 KB |
Output is correct |
21 |
Correct |
15 ms |
1152 KB |
Output is correct |
22 |
Correct |
8 ms |
896 KB |
Output is correct |
23 |
Correct |
13 ms |
1152 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
896 KB |
Output is correct |
28 |
Correct |
11 ms |
1280 KB |
Output is correct |
29 |
Correct |
14 ms |
1152 KB |
Output is correct |
30 |
Correct |
13 ms |
1152 KB |
Output is correct |
31 |
Correct |
11 ms |
1152 KB |
Output is correct |
32 |
Correct |
11 ms |
1152 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
484 KB |
Output is correct |
35 |
Correct |
9 ms |
896 KB |
Output is correct |
36 |
Correct |
10 ms |
1092 KB |
Output is correct |
37 |
Correct |
9 ms |
896 KB |
Output is correct |
38 |
Correct |
9 ms |
996 KB |
Output is correct |
39 |
Correct |
12 ms |
1280 KB |
Output is correct |
40 |
Correct |
2 ms |
256 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
12 ms |
1280 KB |
Output is correct |
19 |
Correct |
12 ms |
1152 KB |
Output is correct |
20 |
Correct |
12 ms |
1152 KB |
Output is correct |
21 |
Correct |
15 ms |
1152 KB |
Output is correct |
22 |
Correct |
8 ms |
896 KB |
Output is correct |
23 |
Correct |
13 ms |
1152 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
896 KB |
Output is correct |
28 |
Correct |
11 ms |
1280 KB |
Output is correct |
29 |
Correct |
14 ms |
1152 KB |
Output is correct |
30 |
Correct |
13 ms |
1152 KB |
Output is correct |
31 |
Correct |
11 ms |
1152 KB |
Output is correct |
32 |
Correct |
11 ms |
1152 KB |
Output is correct |
33 |
Correct |
3 ms |
384 KB |
Output is correct |
34 |
Correct |
3 ms |
484 KB |
Output is correct |
35 |
Correct |
9 ms |
896 KB |
Output is correct |
36 |
Correct |
10 ms |
1092 KB |
Output is correct |
37 |
Correct |
9 ms |
896 KB |
Output is correct |
38 |
Correct |
9 ms |
996 KB |
Output is correct |
39 |
Correct |
12 ms |
1280 KB |
Output is correct |
40 |
Correct |
2 ms |
256 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
3 ms |
384 KB |
Output is correct |
46 |
Correct |
2 ms |
384 KB |
Output is correct |
47 |
Correct |
2 ms |
384 KB |
Output is correct |
48 |
Correct |
2 ms |
384 KB |
Output is correct |
49 |
Correct |
3 ms |
384 KB |
Output is correct |
50 |
Correct |
2 ms |
384 KB |
Output is correct |
51 |
Correct |
2 ms |
384 KB |
Output is correct |
52 |
Correct |
2 ms |
384 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
3 ms |
384 KB |
Output is correct |
55 |
Correct |
2 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
3 ms |
384 KB |
Output is correct |
58 |
Execution timed out |
1068 ms |
53936 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |