This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Going for full
*/
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
}
void ran() {
srand(chrono::steady_clock::now().time_since_epoch().count());
}
long long get_rand() {
long long a = rand();
long long b = rand();
return a * (RAND_MAX + 1ll) + b;
}
void usaco() {
freopen("problem.in", "r", stdin);
freopen("problem.out", "w", stdout);
}
template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
#define endl '\n'
// #define double long double
// #define int long long
// const int MOD = 1000 * 1000 * 1000 + 7;
// const int MOD = 998244353;
// #define cerr if(0) cerr
#define debug(x) cerr << #x << ": " << x << endl;
int n;
string s;
int r, g, b;
const int MXN = 405;
int dp[MXN][MXN][MXN][4];
vector<int> rs, gs, bs;
int pfr[MXN];
int pfg[MXN];
int pfb[MXN];
signed main() {
cin >> n >> s;
int rc = 0, bc = 0, gc = 0;
for (int i=0; i<n; i++) {
if (s[i] == 'R') {
rs.push_back(i+1);
r++;
rc++;
}
if (s[i] == 'G') {
gs.push_back(i+1);
g++;
gc++;
}
if (s[i] == 'Y') {
bs.push_back(i+1);
b++;
bc++;
}
pfr[i+1] = r;
pfg[i+1] = g;
pfb[i+1] = b;
}
s = ' ' + s;
for (int ind=1; ind<=n; ind++) {
for (int r=0; r<=rc; r++) {
for (int g=0; g<=gc; g++) {
int b = ind - r - g;
if (b >= 0 && b <= bc) {
for (int whi=1; whi<=3; whi++) {
int re = INT_MAX / 2;
if (whi != 1 && r > 0) {
int h = dp[ind-1][r-1][g][1];
int l = rs[r-1];
h += max(l + max(g - pfg[l], 0) + max(b - pfb[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 2 && g > 0) {
int h = dp[ind-1][r][g-1][2];
int l = gs[g-1];
h += max(l + max(b - pfb[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 3 && b > 0) {
int h = dp[ind-1][r][g][3];
int l = bs[b-1];
h += max(l + max(g - pfg[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
dp[ind][r][g][whi] = re;
}
}
}
}
}
int re = dp[n][r][g][1];
for (int i=1; i<=3; i++) re = min(re, dp[n][r][g][i]);
if (re > n * n * 2) {
cout << -1 << endl;
}
else {
cout << re << endl;
}
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'void usaco()':
joi2019_ho_t3.cpp:18:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("problem.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:19:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("problem.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |