//#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(n + 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 i = 1; i <= n; i++) {
for (ll cr = 0; cr <= pr.back(); cr++) {
for (ll cg = 0; cg <= pg.back(); cg++) {
ll cy = i - cr - cg;
if (cy < 0 || cy > py.back()) {
continue;
}
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[i][cr][cg][lst], dp[i - 1][cr - (lst == 0)][cg - (lst == 1)][olst] + add);
}
}
}
}
}
ll ans = min({dp[n][pr.back()][pg.back()][0], dp[n][pr.back()][pg.back()][1], dp[n][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:112:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:113:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
3420 KB |
Output is correct |
26 |
Correct |
3 ms |
3928 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
4 ms |
3164 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2140 KB |
Output is correct |
36 |
Correct |
2 ms |
1628 KB |
Output is correct |
37 |
Correct |
1 ms |
1372 KB |
Output is correct |
38 |
Correct |
3 ms |
1884 KB |
Output is correct |
39 |
Correct |
2 ms |
1884 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
749 ms |
892716 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
3420 KB |
Output is correct |
26 |
Correct |
3 ms |
3928 KB |
Output is correct |
27 |
Correct |
3 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1884 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1884 KB |
Output is correct |
33 |
Correct |
4 ms |
3164 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2140 KB |
Output is correct |
36 |
Correct |
2 ms |
1628 KB |
Output is correct |
37 |
Correct |
1 ms |
1372 KB |
Output is correct |
38 |
Correct |
3 ms |
1884 KB |
Output is correct |
39 |
Correct |
2 ms |
1884 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
1884 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Execution timed out |
749 ms |
892716 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |