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;
struct data {
int i, j, k, prv;
data() {}
data(int i, int j, int k, int prv):
i(i), j(j), k(k), prv(prv) {}
bool operator<(const data &D) const {
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(i, j, k, prv)))
return f[data(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(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 (stderr)
joi2019_ho_t3.cpp: In function 'int dp(int, int, int, int, int)':
joi2019_ho_t3.cpp:63:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i<r.size() && prv!=0)
~^~~~~~~~~
joi2019_ho_t3.cpp:65:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j<g.size() && prv!=1)
~^~~~~~~~~
joi2019_ho_t3.cpp:67:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k<y.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... |