#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
namespace output {
void pr(int x) {
cout << x;
}
void pr(long x) {
cout << x;
}
void pr(ll x) {
cout << x;
}
void pr(unsigned x) {
cout << x;
}
void pr(unsigned long x) {
cout << x;
}
void pr(unsigned long long x) {
cout << x;
}
void pr(float x) {
cout << x;
}
void pr(double x) {
cout << x;
}
void pr(long double x) {
cout << x;
}
void pr(char x) {
cout << x;
}
void pr(const char * x) {
cout << x;
}
void pr(const string & x) {
cout << x;
}
void pr(bool x) {
pr(x ? "true" : "false");
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
template < class T > void pr(const T & x);
template < class T, class...Ts > void pr(const T & t,
const Ts & ...ts) {
pr(t);
pr(ts...);
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
pr("{", x.f, ", ", x.s, "}");
}
template < class T > void pr(const T & x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1;
for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() {
pr("\n");
} // print w/ spaces
template < class T, class...Ts > void ps(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() {
cout << "]" << endl;
} // debug w/ commas
template < class T, class...Ts > void pc(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
}
#ifdef mikey
using namespace output;
#else
using namespace output;
#define dbg(x...)
#endif
const int MX = 200005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;
int n;
int dp[405][405][405][3];
int pos[405][3];
int psum[405][3];
int main(){
#ifdef mikey
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
string s; cin >> s;
string al = "RGY";
int cnts[3];
for (int i = 0; i < 3; i++) {
pos[0][i] = -1;
int c = 1;
for (int j = 0; j < n; j++) {
if (j != 0) psum[j][i] = psum[j - 1][i];
if (s[j] == al[i]) {
pos[c++][i] = j;
psum[j][i]++;
}
}
cnts[i] = c - 1;
}
auto qry = [&] (int l, int r, int t) {
if (r < l) return 0;
if (l == 0) return psum[r][t];
else return psum[r][t] - psum[l - 1][t];
};
for (int i = 0; i <= cnts[0]; i++)
for (int j = 0; j <= cnts[1]; j++)
for (int k = 0; k <= cnts[2]; k++) {
dp[i][j][k][0] = 1e9;
dp[i][j][k][1] = 1e9;
dp[i][j][k][2] = 1e9;
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= cnts[0]; i++) {
for (int j = 0; j <= cnts[1]; j++) {
for (int k = 0; k <= cnts[2]; k++) {
for (int p = 0; p < 3; p++) {
if (i != cnts[0] && p != 0) {
int add = qry(pos[j][1] + 1, pos[i + 1][0], 1) + qry(pos[k][2] + 1, pos[i + 1][0], 2);
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], dp[i][j][k][p] + add);
}
if (j != cnts[1] && p != 1) {
int add = qry(pos[i][0] + 1, pos[j + 1][1], 0) + qry(pos[k][2] + 1, pos[j + 1][1], 2);
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], dp[i][j][k][p] + add);
}
if (k != cnts[2] && p != 2) {
int add = qry(pos[i][0] + 1, pos[k + 1][2], 0) + qry(pos[j][1] + 1, pos[k + 1][2], 1);
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], dp[i][j][k][p] + add);
}
}
}
}
}
int ans = INT_MAX;
for (int p = 0; p < 3; p++) ans = min(ans, dp[cnts[0]][cnts[1]][cnts[2]][p]);
if (ans == 1e9) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
}
Compilation message
joi2019_ho_t3.cpp:104: warning: "dbg" redefined
104 | #define dbg(x...)
|
joi2019_ho_t3.cpp:97: note: this is the location of the previous definition
97 | #define dbg(x...) pr("[", #x, "] = ["), pc(x);
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
616 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
616 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2432 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2284 KB |
Output is correct |
21 |
Correct |
2 ms |
2412 KB |
Output is correct |
22 |
Correct |
1 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
1 ms |
1644 KB |
Output is correct |
25 |
Correct |
2 ms |
4076 KB |
Output is correct |
26 |
Correct |
2 ms |
4204 KB |
Output is correct |
27 |
Correct |
2 ms |
3052 KB |
Output is correct |
28 |
Correct |
2 ms |
2432 KB |
Output is correct |
29 |
Correct |
2 ms |
2284 KB |
Output is correct |
30 |
Correct |
2 ms |
2284 KB |
Output is correct |
31 |
Correct |
2 ms |
2028 KB |
Output is correct |
32 |
Correct |
2 ms |
2412 KB |
Output is correct |
33 |
Correct |
2 ms |
3948 KB |
Output is correct |
34 |
Correct |
2 ms |
3564 KB |
Output is correct |
35 |
Correct |
2 ms |
2796 KB |
Output is correct |
36 |
Correct |
2 ms |
1900 KB |
Output is correct |
37 |
Correct |
1 ms |
1772 KB |
Output is correct |
38 |
Correct |
2 ms |
2156 KB |
Output is correct |
39 |
Correct |
2 ms |
2412 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
2284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
74 ms |
163044 KB |
Output is correct |
3 |
Correct |
77 ms |
162148 KB |
Output is correct |
4 |
Correct |
76 ms |
163044 KB |
Output is correct |
5 |
Correct |
75 ms |
163044 KB |
Output is correct |
6 |
Correct |
77 ms |
163044 KB |
Output is correct |
7 |
Correct |
76 ms |
162148 KB |
Output is correct |
8 |
Correct |
75 ms |
162148 KB |
Output is correct |
9 |
Correct |
74 ms |
161508 KB |
Output is correct |
10 |
Correct |
75 ms |
163044 KB |
Output is correct |
11 |
Correct |
75 ms |
163048 KB |
Output is correct |
12 |
Correct |
21 ms |
44140 KB |
Output is correct |
13 |
Correct |
37 ms |
77156 KB |
Output is correct |
14 |
Correct |
53 ms |
111460 KB |
Output is correct |
15 |
Correct |
76 ms |
163044 KB |
Output is correct |
16 |
Correct |
74 ms |
163044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
616 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2432 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2284 KB |
Output is correct |
21 |
Correct |
2 ms |
2412 KB |
Output is correct |
22 |
Correct |
1 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
1 ms |
1644 KB |
Output is correct |
25 |
Correct |
2 ms |
4076 KB |
Output is correct |
26 |
Correct |
2 ms |
4204 KB |
Output is correct |
27 |
Correct |
2 ms |
3052 KB |
Output is correct |
28 |
Correct |
2 ms |
2432 KB |
Output is correct |
29 |
Correct |
2 ms |
2284 KB |
Output is correct |
30 |
Correct |
2 ms |
2284 KB |
Output is correct |
31 |
Correct |
2 ms |
2028 KB |
Output is correct |
32 |
Correct |
2 ms |
2412 KB |
Output is correct |
33 |
Correct |
2 ms |
3948 KB |
Output is correct |
34 |
Correct |
2 ms |
3564 KB |
Output is correct |
35 |
Correct |
2 ms |
2796 KB |
Output is correct |
36 |
Correct |
2 ms |
1900 KB |
Output is correct |
37 |
Correct |
1 ms |
1772 KB |
Output is correct |
38 |
Correct |
2 ms |
2156 KB |
Output is correct |
39 |
Correct |
2 ms |
2412 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
2284 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
74 ms |
163044 KB |
Output is correct |
44 |
Correct |
77 ms |
162148 KB |
Output is correct |
45 |
Correct |
76 ms |
163044 KB |
Output is correct |
46 |
Correct |
75 ms |
163044 KB |
Output is correct |
47 |
Correct |
77 ms |
163044 KB |
Output is correct |
48 |
Correct |
76 ms |
162148 KB |
Output is correct |
49 |
Correct |
75 ms |
162148 KB |
Output is correct |
50 |
Correct |
74 ms |
161508 KB |
Output is correct |
51 |
Correct |
75 ms |
163044 KB |
Output is correct |
52 |
Correct |
75 ms |
163048 KB |
Output is correct |
53 |
Correct |
21 ms |
44140 KB |
Output is correct |
54 |
Correct |
37 ms |
77156 KB |
Output is correct |
55 |
Correct |
53 ms |
111460 KB |
Output is correct |
56 |
Correct |
76 ms |
163044 KB |
Output is correct |
57 |
Correct |
74 ms |
163044 KB |
Output is correct |
58 |
Correct |
84 ms |
76260 KB |
Output is correct |
59 |
Correct |
96 ms |
96612 KB |
Output is correct |
60 |
Correct |
93 ms |
87908 KB |
Output is correct |
61 |
Correct |
93 ms |
84964 KB |
Output is correct |
62 |
Correct |
79 ms |
160612 KB |
Output is correct |
63 |
Correct |
86 ms |
160228 KB |
Output is correct |
64 |
Correct |
97 ms |
150112 KB |
Output is correct |
65 |
Correct |
102 ms |
138552 KB |
Output is correct |
66 |
Correct |
89 ms |
86116 KB |
Output is correct |
67 |
Correct |
92 ms |
82920 KB |
Output is correct |
68 |
Correct |
89 ms |
88676 KB |
Output is correct |
69 |
Correct |
90 ms |
85604 KB |
Output is correct |
70 |
Correct |
103 ms |
92644 KB |
Output is correct |
71 |
Correct |
89 ms |
83940 KB |
Output is correct |
72 |
Correct |
33 ms |
41608 KB |
Output is correct |
73 |
Correct |
5 ms |
2924 KB |
Output is correct |