This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 (stderr)
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) {
~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |