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];
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;
cnt[i] = g[i].size();
}
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
puts("-1"); exit(0);
}
int c[5];
for(c[0] = 0; c[0] <= cnt[0]; c[0]++) {
for(c[1] = 0; c[1] <= cnt[1]; c[1]++) {
for(c[2] = 0; c[2] <= cnt[2]; c[2]++) {
vector<int> tmp;
for(int id = 0; id < 3; id++) {
for(int l = 0; l < c[id]; l++)
tmp.emplace_back(g[id][l]);
}
sort(tmp.begin(), tmp.end());
for(int id = 0; id < 3; id++) {
if(c[id] != 0) {
where[id][c[0]][c[1]][c[2]] = lower_bound(tmp.begin(), tmp.end(), g[id][c[id] - 1]) - tmp.begin() + 1;
}
}
}
}
}
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]]);
}
cout << ans << endl;
}
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:56: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:45: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:46: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... |