This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;
#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a, val) memset(a, val, sizeof (a))
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream &os, const pair<F, S> &p) {
return os << "(" << p.first << ", " << p.second << ")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
std::priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
const int maxn = 407;
const int inf = 1e8;
int where[3][maxn >> 1][maxn >> 1][maxn >> 1];
int a[maxn], cnt[5], n;
vector<int> g[5];
int dp[3][maxn >> 1][maxn >> 1][maxn >> 1];
int p[3][maxn];
int32_t main () {
// freopen("in", "r", stdin);
char str[maxn];
scanf("%d", &n);
scanf("%s", str + 1);
for(int i = 1; i <= n; i++) {
if(str[i] == 'R') g[0].emplace_back(i);
else if(str[i] == 'G') g[1].emplace_back(i);
else g[2].emplace_back(i);
}
for(int i = 0; i < 3; i++) {
for(int j : g[i]) a[j] = i, p[i][j] = 1;
cnt[i] = g[i].size();
for(int j = 1; j <= n; j++) p[i][j] += p[i][j - 1];
}
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
puts("-1"); exit(0);
}
for(int i = 0; i <= cnt[0]; i++) {
for(int j = 0; j <= cnt[1]; j++) {
for(int k = 0; k <= cnt[2]; k++) {
if(i != 0) where[0][i][j][k] = (j ? min(j, p[1][g[0][i - 1] - 1]) : 0) + (k ? min(k, p[2][g[0][i - 1] - 1]) : 0) + i;
if(j != 0) where[1][i][j][k] = (i ? min(i, p[0][g[1][j - 1] - 1]) : 0) + (k ? min(k, p[2][g[1][j - 1] - 1]) : 0) + j;
if(k != 0) where[2][i][j][k] = (j ? min(j, p[1][g[2][k - 1] - 1]) : 0) + (i ? min(i, p[0][g[2][k - 1] - 1]) : 0) + k;
}
}
}
for(int i = 0; i <= cnt[0]; i++) {
for(int j = 0; j <= cnt[1]; j++) {
for(int k = 0; k <= cnt[2]; k++) if(i != 0 or j != 0 or k != 0) {
for(int l = 0; l < 3; l++) {
dp[l][i][j][k] = inf;
if(l != 0 and i > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[0][i][j][k] + dp[0][i - 1][j][k]);
if(l != 1 and j > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[1][i][j][k] + dp[1][i][j - 1][k]);
if(l != 2 and k > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[2][i][j][k] + dp[2][i][j][k - 1]);
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; i++) {
ans = min(ans, dp[i][cnt[0]][cnt[1]][cnt[2]]);
}
printf("%d\n", ans);
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int myrand(int, int)':
joi2019_ho_t3.cpp:18:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
joi2019_ho_t3.cpp:18:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:58:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str + 1);
~~~~~^~~~~~~~~~~~~~~
# | 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... |