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>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 400 + 2;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
struct Fenwick {
int n;
vector<int> v;
Fenwick(int _n = 0) {
n = _n;
v.assign(n + 2, 0);
}
void update(int x, int c) {
for(; x <= n; x += x & -x) v[x] += c;
}
int get(int x) {
int ans = 0;
for(; x > 0; x -= x & -x) ans += v[x];
return ans;
}
} fw;
int n;
int a[N];
string s;
void read() {
cin >> n >> s;
for(int i = 0; i < n; i++) {
if (s[i] == 'R') a[i + 1] = 0;
else if (s[i] == 'G') a[i + 1] = 1;
else a[i + 1] = 2;
}
}
bool check3() {
for(int i = 1; i <= n; i++) if (a[i] == 2) return false;
return true;
}
void sub3() {
vector<int> cnt[2];
for(int i = 1; i <= n; i++) cnt[a[i]].push_back(i);
if (abs(SZ(cnt[0]) - SZ(cnt[1])) > 1) {
cout << -1;
return;
}
if (n & 1) {
if (SZ(cnt[0]) < SZ(cnt[1])) swap(cnt[0], cnt[1]);
reverse(ALL(cnt[0]));
reverse(ALL(cnt[1]));
int ans = 0;
for(int i = 1; i <= n; i++) {
int c = 1;
if (i & 1) c = 0;
int p = cnt[c].back(); cnt[c].pop_back();
for(int j = 0; j < 2; j++) {
for(auto &pos: cnt[j]) if (pos < p) ++pos;
}
ans += p - i;
// cerr << p << ' ' << i << ' ' << ans << endl;
}
cout << ans;
}
else {
vector<int> tmp[2];
tmp[0] = cnt[0];
tmp[1] = cnt[1];
int ans = inf;
for(int iter = 0; iter < 2; iter++) {
cnt[0] = tmp[0];
cnt[1] = tmp[1];
reverse(ALL(cnt[0]));
reverse(ALL(cnt[1]));
int res = 0;
for(int i = 1; i <= n; i++) {
int c = 1;
if (i & 1) c = 0;
int p = cnt[c].back(); cnt[c].pop_back();
for(int j = 0; j < 2; j++) {
for(auto &pos: cnt[j]) if (pos < p) ++pos;
}
res += p - i;
}
ans = min(ans, res);
swap(tmp[0], tmp[1]);
}
cout << ans;
}
}
void sub1() {
vector<int> cnt[3], tmp[3];
for(int i = 1; i <= n; i++) cnt[a[i]].push_back(i);
for(int i = 0; i < 3; i++) {
reverse(ALL(cnt[i]));
tmp[i] = cnt[i];
}
sort(a + 1, a + n + 1);
int ans = inf;
do {
for(int i = 0; i < 3; i++) cnt[i] = tmp[i];
int res = 0;
for(int i = 1; i <= n; i++) {
if (i > 1 && a[i] == a[i - 1]) {
res = inf;
break;
}
int p = cnt[a[i]].back(); cnt[a[i]].pop_back();
for(int j = 0; j < 3; j++) {
for(auto &pos: cnt[j]) if (pos < p) ++pos;
}
res += p - i;
// cerr << p << ' ' << i << ' ' << ans << endl;
}
ans = min(ans, res);
}
while(next_permutation(a + 1, a + n + 1));
cout << (ans == inf ? -1 : ans);
}
void sub2() {
fw = Fenwick(n);
vector<int> pos[3];
for(int i = 1; i <= n; i++) pos[a[i]].push_back(i);
vector<vector<vector<int>>> dp[3];
for(int i = 0; i < 3; i++) {
dp[i].assign(SZ(pos[0]) + 1, vector<vector<int>>(SZ(pos[1]) + 1, vector<int>(SZ(pos[2]) + 1, inf)));
}
vector<tuple<int, int, int>> upd[n + 1];
for(int i = 0; i <= SZ(pos[0]); i++) {
for(int j = 0; j <= SZ(pos[1]); j++) {
for(int z = 0; z <= SZ(pos[2]); z++) {
// cerr << i + j + z << ' ' << i << ' ' << j << ' ' << z << endl;
upd[i + j + z].push_back({i, j, z});
}
}
}
for(int i = 0; i < 2; i++) dp[i][0][0][0] = 0;
auto countLess = [&](tuple<int, int, int> sz, int p) -> int {
// return 0;
int cnt = 0;
int s[] = {get<0>(sz), get<1>(sz), get<2>(sz)};
for(int i = 0; i < 3; i++) {
int t = lower_bound(pos[i].begin(), pos[i].begin() + s[i], p) - pos[i].begin();
cnt += s[i] - t;
}
// if (p == 1) cout << cnt << endl;
// if (p == 1) cout << cnt << endl;
return cnt;
};
for(int i = 0; i < n; i++) {
for(auto j: upd[i]) {
int c0 = get<0>(j);
int c1 = get<1>(j);
int c2 = get<2>(j);
for(int last = 0; last < 3; last++) {
if (dp[last][c0][c1][c2] < inf) {
// cerr << c0 << ' ' << c1 << ' ' << c2 << ' ' << last << ' ' << dp[last][c0][c1][c2] << endl;
tuple<int, int, int> sz = {c0, c1, c2};
int tmp = dp[last][c0][c1][c2];
if (last != 0 && c0 < SZ(pos[0])) {
int p = pos[0][c0];
int less = countLess(sz, p);
int cost = p + less - (i + 1);
minimize(dp[0][c0 + 1][c1][c2], tmp + cost);
}
if (last != 1 && c1 < SZ(pos[1])) {
int p = pos[1][c1];
int less = countLess(sz, p);
int cost = p + less - (i + 1);
minimize(dp[1][c0][c1 + 1][c2], tmp + cost);
}
if (last != 2 && c2 < SZ(pos[2])) {
int p = pos[2][c2];
int less = countLess(sz, p);
int cost = p + less - (i + 1);
minimize(dp[2][c0][c1][c2 + 1], tmp + cost);
}
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; i++) ans = min(ans, dp[i][SZ(pos[0])][SZ(pos[1])][SZ(pos[2])]);
cout << (ans == inf ? -1 : ans);
}
void solve() {
if (check3()) sub3();
else if (n <= 15) sub1();
else if (n <= 60) sub2();
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
// rmq - rmq2d
// hash
// fw - fw2d
// segtree
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:205:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
205 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:206:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
206 | freopen(TASK".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... |