//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;
struct data {
int cur, i, j, k, prv;
data() {}
data(int cur, int i, int j, int k, int prv):
cur(cur), i(i), j(j), k(k), prv(prv) {}
bool operator<(const data &D) const {
if (cur!=D.cur)
return cur<D.cur;
if (i!=D.i)
return i<D.i;
if (j!=D.j)
return j<D.j;
if (k!=D.k)
return k<D.k;
return prv<D.prv;
}
};
int n, a[MAX_N];
map<data, int> f;
vector<int> r, g, y;
void readInput() {
cin >> n;
for (int i=1; i<=n; ++i) {
char c;
cin >> c;
if (c=='R')
r.push_back(i);
else if (c=='G')
g.push_back(i);
else
y.push_back(i);
}
}
int dp(int cur, int i, int j, int k, int prv) {
if (cur==n+1)
return 0;
if (f.count(data(cur, i, j, k, prv)))
return f[data(cur, i, j, k, prv)];
int res = INF;
if (i<r.size() && prv!=0)
res = min(res, dp(cur+1, i+1, j, k, 0) + max(0, r[i]-cur));
if (j<g.size() && prv!=1)
res = min(res, dp(cur+1, i, j+1, k, 1) + max(0, g[j]-cur));
if (k<y.size() && prv!=2)
res = min(res, dp(cur+1, i, j, k+1, 2) + max(0, y[k]-cur));
return f[data(cur, 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:65:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i<r.size() && prv!=0)
~^~~~~~~~~
joi2019_ho_t3.cpp:67:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j<g.size() && prv!=1)
~^~~~~~~~~
joi2019_ho_t3.cpp:69:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k<y.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 |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 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 |
3 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
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 |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 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 |
3 ms |
384 KB |
Output is correct |
11 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |