This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template<class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template<class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = [", _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
const int mx = 405;
int dp[mx][mx][mx][3], pref[mx][3], pos[3][mx], ct[3];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; string s;
cin >> n >> s;
for (int q = 1; q <= n; q++) {
for (int w = 0; w < 3; w++) pref[q][w] = pref[q-1][w];
if (s[q-1] == 'R') {
pref[q][0]++;
ct[0]++;
pos[0][ct[0]] = q;
} else if (s[q-1] == 'G') {
pref[q][1]++;
ct[1]++;
pos[1][ct[1]] = q;
} else {
pref[q][2]++;
ct[2]++;
pos[2][ct[2]] = q;
}
}
int inf = 1e8;
for (int q = 0; q <= ct[0]; q++) {
for (int w = 0; w <= ct[1]; w++) {
for (int e = 0; e <= ct[2]; e++) {
for (int r = 0; r < 3; r++) {
dp[q][w][e][r] = inf;
}
}
}
}
for (int q = 0; q < 3; q++) dp[0][0][0][q] = 0;
for (int q = 0; q <= ct[0]; q++) {
for (int w = 0; w <= ct[1]; w++) {
for (int e = 0; e <= ct[2]; e++) {
for (int r = 0; r < 3; r++) {
if (r == 0 && q == 0) continue;
if (r == 1 && w == 0) continue;
if (r == 2 && e == 0) continue;
int tot = 0;
if (r == 0) {
tot += max(0, pref[pos[0][q]][1]-pref[pos[1][w]][1]);
tot += max(0, pref[pos[0][q]][2]-pref[pos[2][e]][2]);
tot += min(dp[q-1][w][e][1], dp[q-1][w][e][2]);
} else if (r == 1) {
tot += max(0, pref[pos[1][w]][0]-pref[pos[0][q]][0]);
tot += max(0, pref[pos[1][w]][2]-pref[pos[2][e]][2]);
tot += min(dp[q][w-1][e][0], dp[q][w-1][e][2]);
} else {
tot += max(0, pref[pos[2][e]][0]-pref[pos[0][q]][0]);
tot += max(0, pref[pos[2][e]][1]-pref[pos[1][w]][1]);
tot += min(dp[q][w][e-1][0], dp[q][w][e-1][1]);
}
dp[q][w][e][r] = min(dp[q][w][e][r], tot);
}
}
}
}
int ans = inf;
for (int q = 0; q < 3; q++) ans = min(ans, dp[ct[0]][ct[1]][ct[2]][q]);
if (ans == inf) cout << "-1\n";
else cout << ans << '\n';
return 0;
}
# | 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... |