//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
void solve() {
ll n;
cin >> n;
string s;
cin >> s;
vector<ll> pr(1, 0), pg(1, 0), py(1, 0);
vector<ll> posr, posg, posy;
for (ll i = 0; i < n; i++) {
pr.push_back(pr.back() + (s[i] == 'R'));
pg.push_back(pg.back() + (s[i] == 'G'));
py.push_back(py.back() + (s[i] == 'Y'));
if (s[i] == 'R') {
posr.push_back(i);
}
if (s[i] == 'G') {
posg.push_back(i);
}
if (s[i] == 'Y') {
posy.push_back(i);
}
}
vector<vector<vector<vector<ll>>>> dp(py.back() + 1, vector<vector<vector<ll>>>(pr.back() + 1, vector<vector<ll>>(pg.back() + 1, vector<ll>(3, INF32))));
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (ll cy = 0; cy <= py.back(); cy++) {
for (ll cr = 0; cr <= pr.back(); cr++) {
for (ll cg = 0; cg <= pg.back(); cg++) {
for (ll lst = 0; lst < 3; lst++) {
if ((lst == 0 && cr == 0) || (lst == 1 && cg == 0) || (lst == 2 && cy == 0)) {
continue;
}
for (ll olst = 0; olst < 3; olst++) {
if (olst == lst) {
continue;
}
ll add = 0;
if (lst == 0) {
add = max(0ll, pg[posr[cr - 1]] - cg) + max(0ll, py[posr[cr - 1]] - cy);
}
if (lst == 1) {
add = max(0ll, pr[posg[cg - 1]] - cr) + max(0ll, py[posg[cg - 1]] - cy);
}
if (lst == 2) {
add = max(0ll, pr[posy[cy - 1]] - cr) + max(0ll, pg[posy[cy - 1]] - cg);
}
assign_min(dp[cy][cr][cg][lst], dp[cy - (lst == 2)][cr - (lst == 0)][cg - (lst == 1)][olst] + add);
}
}
}
}
}
ll ans = min({dp[py.back()][pr.back()][pg.back()][0], dp[py.back()][pr.back()][pg.back()][1], dp[py.back()][pr.back()][pg.back()][2]});
cout << (ans == INF32 ? -1 : ans) << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:108:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:109:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
548 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
548 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
856 KB |
Output is correct |
37 |
Correct |
1 ms |
1112 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
4700 KB |
Output is correct |
3 |
Correct |
5 ms |
4700 KB |
Output is correct |
4 |
Correct |
5 ms |
4700 KB |
Output is correct |
5 |
Correct |
5 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4944 KB |
Output is correct |
7 |
Correct |
5 ms |
4700 KB |
Output is correct |
8 |
Correct |
5 ms |
4700 KB |
Output is correct |
9 |
Correct |
6 ms |
4700 KB |
Output is correct |
10 |
Correct |
5 ms |
4700 KB |
Output is correct |
11 |
Correct |
6 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
3 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
3420 KB |
Output is correct |
15 |
Correct |
5 ms |
4700 KB |
Output is correct |
16 |
Correct |
5 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
548 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
856 KB |
Output is correct |
37 |
Correct |
1 ms |
1112 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
4700 KB |
Output is correct |
44 |
Correct |
5 ms |
4700 KB |
Output is correct |
45 |
Correct |
5 ms |
4700 KB |
Output is correct |
46 |
Correct |
5 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4944 KB |
Output is correct |
48 |
Correct |
5 ms |
4700 KB |
Output is correct |
49 |
Correct |
5 ms |
4700 KB |
Output is correct |
50 |
Correct |
6 ms |
4700 KB |
Output is correct |
51 |
Correct |
5 ms |
4700 KB |
Output is correct |
52 |
Correct |
6 ms |
4700 KB |
Output is correct |
53 |
Correct |
1 ms |
1628 KB |
Output is correct |
54 |
Correct |
3 ms |
2396 KB |
Output is correct |
55 |
Correct |
3 ms |
3420 KB |
Output is correct |
56 |
Correct |
5 ms |
4700 KB |
Output is correct |
57 |
Correct |
5 ms |
4700 KB |
Output is correct |
58 |
Correct |
183 ms |
133292 KB |
Output is correct |
59 |
Correct |
185 ms |
133548 KB |
Output is correct |
60 |
Correct |
179 ms |
132432 KB |
Output is correct |
61 |
Correct |
185 ms |
133972 KB |
Output is correct |
62 |
Correct |
17 ms |
13476 KB |
Output is correct |
63 |
Correct |
28 ms |
21596 KB |
Output is correct |
64 |
Correct |
101 ms |
67440 KB |
Output is correct |
65 |
Correct |
131 ms |
96084 KB |
Output is correct |
66 |
Correct |
182 ms |
131844 KB |
Output is correct |
67 |
Correct |
173 ms |
131668 KB |
Output is correct |
68 |
Correct |
180 ms |
133716 KB |
Output is correct |
69 |
Correct |
179 ms |
134836 KB |
Output is correct |
70 |
Correct |
184 ms |
134488 KB |
Output is correct |
71 |
Correct |
178 ms |
131284 KB |
Output is correct |
72 |
Correct |
49 ms |
35164 KB |
Output is correct |
73 |
Correct |
12 ms |
9052 KB |
Output is correct |