#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;
//#pragma GCC optimization("g", on)
//#pragma GCC optimization("03")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("inline")
//#pragma GCC optimize("-fgcse,-fgcse-lm")
//#pragma GCC optimize("-ftree-pre,-ftree-vrp")
//#pragma GCC optimize("-ffast-math")
//#pragma GCC optimize("-fipa-sra")
//#pragma GCC optimize("-fpeephole2")
//#pragma GCC optimize("-fsched-spec")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
#define aboba ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define br break;
#define sp " "
#define en "\n"
#define pb push_back
#define sz(a) int(a.size())
#define bg begin()
#define ed end()
#define in insert
#define ss second
#define ff first
#define setp(a) cout << fixed; cout << setprecision(a);
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef double db;
typedef tree<
long long,
null_type,
less_equal<long long>,
rb_tree_tag,
tree_order_statistics_node_update> orset;
void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
ll bp(ll x, ll y, ll z) { ll res = 1; while (y) { if (y & 1) { res = (res * x) % z; y--; } x = (x * x) % z; y >>= 1; } return res; }
// C(n, k) = ((fact[n] * bp(fact[k], mod - 2)) % mod * bp(fact[n - k], mod - 2)) % mod;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll lcm(ll a, ll b) { return (a / __gcd(a, b)) * b; }
const ll N = 2e5 + 11;
const ll inf = 1e18 + 7;
ll tt = 1;
ll p[404][4];
ll dp[404][404][404][4];
ll pos[404][4];
void solve() {
ll n; cin >> n;
string s; cin >> s;
s = ' ' + s;
ll g = 0, r = 0, y = 0;
for (int i = 1;i <= n;i++) {
(s[i] == 'R' ? r : (s[i] == 'G' ? g : y))++;
pos[(s[i] == 'R' ? r : (s[i] == 'G' ? g : y))][(s[i] == 'R' ? 1 : (s[i] == 'G' ? 2 : 3))] = i;
p[i][1] = p[i - 1][1];
p[i][2] = p[i - 1][2];
p[i][3] = p[i - 1][3];
p[i][(s[i] == 'R' ? 1 : (s[i] == 'G' ? 2 : 3))]++;
}
for (ll i = 0;i <= r;i++) {
for (ll j = 0;j <= g;j++) {
for (ll k = 0;k <= y;k++) {
for (ll p = 1;p <= 3;p++) dp[i][j][k][p] = inf;
}
}
}
for (ll p = 1;p <= 3;p++) dp[0][0][0][p] = 0;
for (ll i = 0;i <= r;i++) {
for (ll j = 0;j <= g;j++) {
for (ll k = 0;k <= y;k++) {
if (i) {
ll x = pos[i][1] + max(0ll, j - p[pos[i][1]][2]) + max(0ll, k - p[pos[i][1]][3]) - (i + j + k);
// cout << x << sp << i << sp << j << sp << k << sp << 1 << en;
dp[i][j][k][1] = min({dp[i - 1][j][k][2] + x, dp[i - 1][j][k][3] + x, dp[i][j][k][1]});
}
if (j) {
ll x = pos[j][2] + max(0ll, i - p[pos[j][3]][1]) + max(0ll, k - p[pos[j][2]][3]) - (i + j + k);
// cout << x << sp << i << sp << j << sp << k << sp << 2 << en;
dp[i][j][k][2] = min({dp[i][j - 1][k][1] + x, dp[i][j - 1][k][3] + x, dp[i][j][k][2]});
}
if (k) {
ll x = pos[k][3] + max(0ll, j - p[pos[k][3]][2]) + max(0ll, i - p[pos[k][3]][1]) - (i + j + k);
// cout << x << sp << i << sp << j << sp << k << sp << 3 << en;
dp[i][j][k][3] = min({dp[i][j][k - 1][2] + x, dp[i][j][k - 1][1] + x, dp[i][j][k][3]});
}
// cout << i << sp << j << sp << k << en;
// for (ll p = 1;p <= 3;p++) cout << dp[i][j][k][p] << sp;
// cout << en;
}
}
}
ll ans = min({dp[r][g][y][1], dp[r][g][y][2], dp[r][g][y][3]});
cout << (ans == inf ? -1 : ans);
}
// 5
// RRGYY
int main() {
aboba
// freopen("cownomics");
// precalc();
// cin >> tt;
for (int i = 1;i <= tt;i++) {
solve();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void freopen(std::string)':
joi2019_ho_t3.cpp:47:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:47:75: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6616 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6616 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6616 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |