This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Om Namah Shivaya
#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;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
template<typename T>
struct fenwick {
int siz;
vector<T> tree;
fenwick(int n) {
siz = n;
tree = vector<T>(n + 1);
}
int lsb(int x) {
return x & -x;
}
void build(vector<T> &a, int n) {
for (int i = 1; i <= n; ++i) {
int par = i + lsb(i);
tree[i] += a[i];
if (par <= siz) {
tree[par] += tree[i];
}
}
}
void pupd(int i, T v) {
while (i <= siz) {
tree[i] += v;
i += lsb(i);
}
}
T sum(int i) {
T res = 0;
while (i) {
res += tree[i];
i -= lsb(i);
}
return res;
}
T query(int l, int r) {
if (l > r) return 0;
T res = sum(r) - sum(l - 1);
return res;
}
};
void solve(int test_case)
{
ll n; cin >> n;
vector<ll> a(n + 5);
vector<ll> pos[3];
rep1(i, n) {
char ch; cin >> ch;
if (ch == 'R') a[i] = 0, pos[0].pb(i);
else if (ch == 'G') a[i] = 1, pos[1].pb(i);
else a[i] = 2, pos[2].pb(i);
}
ll s1 = sz(pos[0]);
ll s2 = sz(pos[1]);
ll s3 = sz(pos[2]);
ll dp[s1 + 1][s2 + 1][s3 + 1][3];
memset(dp, 0x3f, sizeof dp);
rep(p, 3) {
dp[0][0][0][p] = 0;
}
rep(i, s1 + 1) {
rep(j, s2 + 1) {
fenwick<ll> fenw(n + 5);
rep(i1, i) {
fenw.pupd(pos[0][i1], 1);
}
rep(j1, j) {
fenw.pupd(pos[1][j1], 1);
}
rep(k, s3 + 1) {
if (k - 1 >= 0) {
fenw.pupd(pos[2][k - 1], 1);
}
rep(p, 3) {
rep(x, 3) {
if (x == p) conts;
ll ind = -1;
if (x == 0) {
if (i != s1) {
ind = pos[0][i];
}
}
if (x == 1) {
if (j != s2) {
ind = pos[1][j];
}
}
if (x == 2) {
if (k != s3) {
ind = pos[2][k];
}
}
if (ind == -1) conts;
ll moves = ind - 1 - fenw.query(1, ind - 1);
amin(dp[i + (x == 0)][j + (x == 1)][k + (x == 2)][x], dp[i][j][k][p] + moves);
}
}
}
}
}
ll ans = inf2;
rep(p, 3) {
amin(ans, dp[s1][s2][s3][p]);
}
if (ans >= inf2) ans = -1;
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
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... |