#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
/*
YRGGRRGGYY
dp[i][red][green][yellow][prevCol] = dp[i+1][]
RYRGY
*/
const int N = 401;
int n, arr[N], countPrev[4][N], r = 0, g = 0, y = 0;;
vector<vector<vii>> dp;
vi pos[4];
int sol(int red, int green, int yellow, int prev) {
int idx = (red+green+yellow+1);
if (idx == n+1) return 0;
if (red > r || green > g || yellow > y) return inf;
if (dp[red][green][yellow][prev] != -1) return dp[red][green][yellow][prev];
int ans = inf;
for (int j = 1; j <= 3; j++) {
if (j == prev) continue;
int cost = inf;
if (j == 1) {
red++;
if (red-1 < pos[j].size()) {
int p = pos[j][red-1];
cost = max(0, countPrev[2][p] - green) + max(0, countPrev[3][p] - yellow);
}
}
if (j == 2) {
green++;
if (green-1 < pos[j].size()) {
int p = pos[j][green-1];
cost = max(0, countPrev[1][p] - red) + max(0, countPrev[3][p] - yellow);
}
}
if (j == 3) {
yellow++;
if (yellow-1 < pos[j].size()) {
int p = pos[j][yellow-1];
cost = max(0, countPrev[2][p] - green) + max(0, countPrev[1][p] - red);
}
}
ans = min(ans, sol(red, green, yellow, j) + cost);
if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--;
}
return dp[red][green][yellow][prev] = ans;
}
void sol() {
cin >> n;
string s; cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') arr[i+1] = 1, r++;
if (s[i] == 'G') arr[i+1] = 2, g++;
if (s[i] == 'Y') arr[i+1] = 3, y++;
pos[arr[i+1]].pb(i+1);
countPrev[arr[i+1]][i+1] = 1;
}
dp = vector<vector<vii>>(r+1, vector<vii>(g+1, vii(y+1, vi(4, -1))));
for (int j = 1; j <= 3; j++) {
for (int i = 1; i <= n; i++) {
countPrev[j][i] += countPrev[j][i-1];
}
}
int ans = sol(0, 0, 0, 0);
cout << (ans==inf?-1:ans) << endl;
}
int main () {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
sol();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int sol(int, int, int, int)':
joi2019_ho_t3.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if (red-1 < pos[j].size()) {
| ~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:67:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if (green-1 < pos[j].size()) {
| ~~~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:74:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (yellow-1 < pos[j].size()) {
| ~~~~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:80:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
80 | if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--;
| ^~
joi2019_ho_t3.cpp:80:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
80 | if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
964 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
900 KB |
Output is correct |
29 |
Correct |
2 ms |
812 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
3932 KB |
Output is correct |
3 |
Correct |
4 ms |
3932 KB |
Output is correct |
4 |
Correct |
3 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
4 ms |
3932 KB |
Output is correct |
7 |
Correct |
4 ms |
3932 KB |
Output is correct |
8 |
Correct |
3 ms |
3932 KB |
Output is correct |
9 |
Correct |
4 ms |
3932 KB |
Output is correct |
10 |
Correct |
4 ms |
3824 KB |
Output is correct |
11 |
Correct |
4 ms |
3932 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1932 KB |
Output is correct |
14 |
Correct |
2 ms |
2660 KB |
Output is correct |
15 |
Correct |
6 ms |
3932 KB |
Output is correct |
16 |
Correct |
4 ms |
3992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
964 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
900 KB |
Output is correct |
29 |
Correct |
2 ms |
812 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
4 ms |
3932 KB |
Output is correct |
44 |
Correct |
4 ms |
3932 KB |
Output is correct |
45 |
Correct |
3 ms |
3932 KB |
Output is correct |
46 |
Correct |
3 ms |
3932 KB |
Output is correct |
47 |
Correct |
4 ms |
3932 KB |
Output is correct |
48 |
Correct |
4 ms |
3932 KB |
Output is correct |
49 |
Correct |
3 ms |
3932 KB |
Output is correct |
50 |
Correct |
4 ms |
3932 KB |
Output is correct |
51 |
Correct |
4 ms |
3824 KB |
Output is correct |
52 |
Correct |
4 ms |
3932 KB |
Output is correct |
53 |
Correct |
1 ms |
1372 KB |
Output is correct |
54 |
Correct |
2 ms |
1932 KB |
Output is correct |
55 |
Correct |
2 ms |
2660 KB |
Output is correct |
56 |
Correct |
6 ms |
3932 KB |
Output is correct |
57 |
Correct |
4 ms |
3992 KB |
Output is correct |
58 |
Correct |
247 ms |
133376 KB |
Output is correct |
59 |
Correct |
248 ms |
133572 KB |
Output is correct |
60 |
Correct |
253 ms |
132512 KB |
Output is correct |
61 |
Correct |
252 ms |
133972 KB |
Output is correct |
62 |
Correct |
12 ms |
12564 KB |
Output is correct |
63 |
Correct |
19 ms |
20828 KB |
Output is correct |
64 |
Correct |
78 ms |
66788 KB |
Output is correct |
65 |
Correct |
148 ms |
95568 KB |
Output is correct |
66 |
Correct |
246 ms |
131792 KB |
Output is correct |
67 |
Correct |
259 ms |
131920 KB |
Output is correct |
68 |
Correct |
229 ms |
133716 KB |
Output is correct |
69 |
Correct |
245 ms |
134636 KB |
Output is correct |
70 |
Correct |
261 ms |
134344 KB |
Output is correct |
71 |
Correct |
248 ms |
131192 KB |
Output is correct |
72 |
Correct |
35 ms |
34092 KB |
Output is correct |
73 |
Correct |
11 ms |
10584 KB |
Output is correct |