#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][2]][1]) + max(0ll, k - p[pos[j][2]][3]) - (i + j + k);
// cout << x << sp << i << sp << j << sp << k << sp << 2 << en;
// if (i == 1 && j == 9 && k == 7) cout << pos[j][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); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14848 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
21016 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14848 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
21016 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
51800 KB |
Output is correct |
19 |
Correct |
5 ms |
43612 KB |
Output is correct |
20 |
Correct |
7 ms |
55900 KB |
Output is correct |
21 |
Correct |
5 ms |
47704 KB |
Output is correct |
22 |
Correct |
5 ms |
49752 KB |
Output is correct |
23 |
Correct |
4 ms |
41564 KB |
Output is correct |
24 |
Correct |
3 ms |
31064 KB |
Output is correct |
25 |
Correct |
9 ms |
82776 KB |
Output is correct |
26 |
Correct |
7 ms |
74328 KB |
Output is correct |
27 |
Correct |
7 ms |
62044 KB |
Output is correct |
28 |
Correct |
5 ms |
49756 KB |
Output is correct |
29 |
Correct |
5 ms |
49756 KB |
Output is correct |
30 |
Correct |
6 ms |
43612 KB |
Output is correct |
31 |
Correct |
4 ms |
39516 KB |
Output is correct |
32 |
Correct |
6 ms |
45660 KB |
Output is correct |
33 |
Correct |
7 ms |
66140 KB |
Output is correct |
34 |
Correct |
6 ms |
59996 KB |
Output is correct |
35 |
Correct |
6 ms |
53852 KB |
Output is correct |
36 |
Correct |
5 ms |
45652 KB |
Output is correct |
37 |
Correct |
4 ms |
33372 KB |
Output is correct |
38 |
Correct |
4 ms |
39516 KB |
Output is correct |
39 |
Correct |
5 ms |
45656 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
3 ms |
29020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
132 ms |
689868 KB |
Output is correct |
3 |
Correct |
79 ms |
689744 KB |
Output is correct |
4 |
Correct |
87 ms |
693588 KB |
Output is correct |
5 |
Correct |
81 ms |
693604 KB |
Output is correct |
6 |
Correct |
82 ms |
693584 KB |
Output is correct |
7 |
Correct |
80 ms |
692564 KB |
Output is correct |
8 |
Correct |
79 ms |
692564 KB |
Output is correct |
9 |
Correct |
78 ms |
691796 KB |
Output is correct |
10 |
Correct |
80 ms |
693584 KB |
Output is correct |
11 |
Correct |
84 ms |
693372 KB |
Output is correct |
12 |
Correct |
35 ms |
347216 KB |
Output is correct |
13 |
Correct |
51 ms |
525648 KB |
Output is correct |
14 |
Correct |
64 ms |
648064 KB |
Output is correct |
15 |
Correct |
82 ms |
689748 KB |
Output is correct |
16 |
Correct |
82 ms |
689748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14848 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
21016 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
51800 KB |
Output is correct |
19 |
Correct |
5 ms |
43612 KB |
Output is correct |
20 |
Correct |
7 ms |
55900 KB |
Output is correct |
21 |
Correct |
5 ms |
47704 KB |
Output is correct |
22 |
Correct |
5 ms |
49752 KB |
Output is correct |
23 |
Correct |
4 ms |
41564 KB |
Output is correct |
24 |
Correct |
3 ms |
31064 KB |
Output is correct |
25 |
Correct |
9 ms |
82776 KB |
Output is correct |
26 |
Correct |
7 ms |
74328 KB |
Output is correct |
27 |
Correct |
7 ms |
62044 KB |
Output is correct |
28 |
Correct |
5 ms |
49756 KB |
Output is correct |
29 |
Correct |
5 ms |
49756 KB |
Output is correct |
30 |
Correct |
6 ms |
43612 KB |
Output is correct |
31 |
Correct |
4 ms |
39516 KB |
Output is correct |
32 |
Correct |
6 ms |
45660 KB |
Output is correct |
33 |
Correct |
7 ms |
66140 KB |
Output is correct |
34 |
Correct |
6 ms |
59996 KB |
Output is correct |
35 |
Correct |
6 ms |
53852 KB |
Output is correct |
36 |
Correct |
5 ms |
45652 KB |
Output is correct |
37 |
Correct |
4 ms |
33372 KB |
Output is correct |
38 |
Correct |
4 ms |
39516 KB |
Output is correct |
39 |
Correct |
5 ms |
45656 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
3 ms |
29020 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
132 ms |
689868 KB |
Output is correct |
44 |
Correct |
79 ms |
689744 KB |
Output is correct |
45 |
Correct |
87 ms |
693588 KB |
Output is correct |
46 |
Correct |
81 ms |
693604 KB |
Output is correct |
47 |
Correct |
82 ms |
693584 KB |
Output is correct |
48 |
Correct |
80 ms |
692564 KB |
Output is correct |
49 |
Correct |
79 ms |
692564 KB |
Output is correct |
50 |
Correct |
78 ms |
691796 KB |
Output is correct |
51 |
Correct |
80 ms |
693584 KB |
Output is correct |
52 |
Correct |
84 ms |
693372 KB |
Output is correct |
53 |
Correct |
35 ms |
347216 KB |
Output is correct |
54 |
Correct |
51 ms |
525648 KB |
Output is correct |
55 |
Correct |
64 ms |
648064 KB |
Output is correct |
56 |
Correct |
82 ms |
689748 KB |
Output is correct |
57 |
Correct |
82 ms |
689748 KB |
Output is correct |
58 |
Correct |
66 ms |
462160 KB |
Output is correct |
59 |
Correct |
75 ms |
550504 KB |
Output is correct |
60 |
Correct |
72 ms |
523652 KB |
Output is correct |
61 |
Correct |
72 ms |
503628 KB |
Output is correct |
62 |
Correct |
84 ms |
691420 KB |
Output is correct |
63 |
Correct |
88 ms |
688692 KB |
Output is correct |
64 |
Correct |
94 ms |
679304 KB |
Output is correct |
65 |
Correct |
84 ms |
656596 KB |
Output is correct |
66 |
Correct |
73 ms |
511312 KB |
Output is correct |
67 |
Correct |
65 ms |
449960 KB |
Output is correct |
68 |
Correct |
71 ms |
519528 KB |
Output is correct |
69 |
Correct |
71 ms |
507220 KB |
Output is correct |
70 |
Correct |
76 ms |
527952 KB |
Output is correct |
71 |
Correct |
71 ms |
525800 KB |
Output is correct |
72 |
Correct |
68 ms |
650832 KB |
Output is correct |
73 |
Correct |
3 ms |
19036 KB |
Output is correct |