#include <bits/stdc++.h>
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define sz(v) ((ll) (v).size())
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define F first
#define S second
#define pii(x, y) make_pair(x, y)
#define __builtin_popcount __builtin_popcountll
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define lg(x) (63 - __builtin_clz(x))
template <class X, class Y>
bool minimize(X &x, const Y &y)
{
if (x > y) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y)
{
if (x < y) {x = y; return 1;}
return 0;
}
using namespace std;
typedef long long ll;
const int N = 400 + 5;
const int M = 6e5;
const int mod = 998244353;
const int INF = 1e9 + 7;
const ll oo = 2e18;
const double eps = 1e-1;
const long double pi = 2*acos(0.0);
int n, cnt[N][3], mp[100];
vector<int> v[3];
string s;
void Input(void)
{
mp['R'] = 0, mp['G'] = 1, mp['Y'] = 2;
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i <= n; i ++)
{
for (int j = 0; j < 3; j ++) cnt[i][j] = cnt[i - 1][j];
cnt[i][mp[s[i]]] ++;
v[mp[s[i]]].push_back(i);
}
}
int dp[401][401][401][3];
int cost(int a, int b, int c, int pos)
{
int res = 0;
res += max(0, a - cnt[pos][0]);
res += max(0, b - cnt[pos][1]);
res += max(0, c - cnt[pos][2]);
return res;
}
void solve(void)
{
memset(dp, 0x3f, sizeof dp);
for (int x = 0; x < 3; x ++) dp[0][0][0][x] = 0;
int N0 = cnt[n][0], N1 = cnt[n][1], N2 = cnt[n][2];
for (int i = 0; i <= N0; i ++)
for (int j = 0; j <= N1; j ++)
for (int k = 0; k <= N2; k ++)
for (int x = 0; x < 3; x ++)
if (dp[i][j][k][x] < INF)
{
if (x != 0 && i < N0)
minimize(dp[i + 1][j][k][0], dp[i][j][k][x] + cost(i, j, k, v[0][i]));
if (x != 1 && j < N1)
minimize(dp[i][j + 1][k][1], dp[i][j][k][x] + cost(i, j, k, v[1][j]));
if (x != 2 && k < N2)
minimize(dp[i][j][k + 1][2], dp[i][j][k][x] + cost(i, j, k, v[2][k]));
}
int ans = INF;
for (int x = 0; x < 3; x ++) minimize(ans, dp[N0][N1][N2][x]);
cout << (ans != INF ? ans : -1);
}
int main()
{
std::ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("mixpotions.inp", "r", stdin);
//freopen("mixpotions.out", "w", stdout);
int test = 1;
//cin >> test;
while (test --)
{
Input();
solve();
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'void Input()':
joi2019_ho_t3.cpp:52:23: warning: array subscript has type 'char' [-Wchar-subscripts]
52 | cnt[i][mp[s[i]]] ++;
| ^
joi2019_ho_t3.cpp:53:18: warning: array subscript has type 'char' [-Wchar-subscripts]
53 | v[mp[s[i]]].push_back(i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
757332 KB |
Output is correct |
2 |
Correct |
162 ms |
757320 KB |
Output is correct |
3 |
Correct |
155 ms |
757328 KB |
Output is correct |
4 |
Correct |
151 ms |
757372 KB |
Output is correct |
5 |
Correct |
153 ms |
757332 KB |
Output is correct |
6 |
Correct |
142 ms |
757476 KB |
Output is correct |
7 |
Correct |
134 ms |
757424 KB |
Output is correct |
8 |
Correct |
133 ms |
757328 KB |
Output is correct |
9 |
Correct |
135 ms |
757328 KB |
Output is correct |
10 |
Correct |
133 ms |
757516 KB |
Output is correct |
11 |
Correct |
135 ms |
757332 KB |
Output is correct |
12 |
Correct |
133 ms |
757324 KB |
Output is correct |
13 |
Correct |
134 ms |
757524 KB |
Output is correct |
14 |
Correct |
124 ms |
757308 KB |
Output is correct |
15 |
Correct |
126 ms |
757492 KB |
Output is correct |
16 |
Correct |
108 ms |
757524 KB |
Output is correct |
17 |
Correct |
98 ms |
757524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
757332 KB |
Output is correct |
2 |
Correct |
162 ms |
757320 KB |
Output is correct |
3 |
Correct |
155 ms |
757328 KB |
Output is correct |
4 |
Correct |
151 ms |
757372 KB |
Output is correct |
5 |
Correct |
153 ms |
757332 KB |
Output is correct |
6 |
Correct |
142 ms |
757476 KB |
Output is correct |
7 |
Correct |
134 ms |
757424 KB |
Output is correct |
8 |
Correct |
133 ms |
757328 KB |
Output is correct |
9 |
Correct |
135 ms |
757328 KB |
Output is correct |
10 |
Correct |
133 ms |
757516 KB |
Output is correct |
11 |
Correct |
135 ms |
757332 KB |
Output is correct |
12 |
Correct |
133 ms |
757324 KB |
Output is correct |
13 |
Correct |
134 ms |
757524 KB |
Output is correct |
14 |
Correct |
124 ms |
757308 KB |
Output is correct |
15 |
Correct |
126 ms |
757492 KB |
Output is correct |
16 |
Correct |
108 ms |
757524 KB |
Output is correct |
17 |
Correct |
98 ms |
757524 KB |
Output is correct |
18 |
Correct |
101 ms |
757324 KB |
Output is correct |
19 |
Correct |
91 ms |
757484 KB |
Output is correct |
20 |
Correct |
90 ms |
757492 KB |
Output is correct |
21 |
Correct |
90 ms |
757400 KB |
Output is correct |
22 |
Correct |
89 ms |
757376 KB |
Output is correct |
23 |
Correct |
88 ms |
757408 KB |
Output is correct |
24 |
Correct |
86 ms |
757500 KB |
Output is correct |
25 |
Correct |
88 ms |
757328 KB |
Output is correct |
26 |
Correct |
87 ms |
757496 KB |
Output is correct |
27 |
Correct |
87 ms |
757436 KB |
Output is correct |
28 |
Correct |
86 ms |
757392 KB |
Output is correct |
29 |
Correct |
88 ms |
757332 KB |
Output is correct |
30 |
Correct |
87 ms |
757328 KB |
Output is correct |
31 |
Correct |
87 ms |
757432 KB |
Output is correct |
32 |
Correct |
86 ms |
757328 KB |
Output is correct |
33 |
Correct |
86 ms |
757332 KB |
Output is correct |
34 |
Correct |
87 ms |
757524 KB |
Output is correct |
35 |
Correct |
88 ms |
757428 KB |
Output is correct |
36 |
Correct |
88 ms |
757408 KB |
Output is correct |
37 |
Correct |
86 ms |
757332 KB |
Output is correct |
38 |
Correct |
84 ms |
757384 KB |
Output is correct |
39 |
Correct |
88 ms |
757508 KB |
Output is correct |
40 |
Correct |
88 ms |
757552 KB |
Output is correct |
41 |
Correct |
86 ms |
757528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
757332 KB |
Output is correct |
2 |
Correct |
87 ms |
757372 KB |
Output is correct |
3 |
Correct |
88 ms |
757328 KB |
Output is correct |
4 |
Correct |
88 ms |
757700 KB |
Output is correct |
5 |
Correct |
98 ms |
757428 KB |
Output is correct |
6 |
Correct |
89 ms |
757332 KB |
Output is correct |
7 |
Correct |
87 ms |
757332 KB |
Output is correct |
8 |
Correct |
88 ms |
757332 KB |
Output is correct |
9 |
Correct |
90 ms |
757332 KB |
Output is correct |
10 |
Correct |
86 ms |
757332 KB |
Output is correct |
11 |
Correct |
88 ms |
757524 KB |
Output is correct |
12 |
Correct |
87 ms |
757320 KB |
Output is correct |
13 |
Correct |
87 ms |
757488 KB |
Output is correct |
14 |
Correct |
87 ms |
757312 KB |
Output is correct |
15 |
Correct |
88 ms |
757332 KB |
Output is correct |
16 |
Correct |
90 ms |
757532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
757332 KB |
Output is correct |
2 |
Correct |
162 ms |
757320 KB |
Output is correct |
3 |
Correct |
155 ms |
757328 KB |
Output is correct |
4 |
Correct |
151 ms |
757372 KB |
Output is correct |
5 |
Correct |
153 ms |
757332 KB |
Output is correct |
6 |
Correct |
142 ms |
757476 KB |
Output is correct |
7 |
Correct |
134 ms |
757424 KB |
Output is correct |
8 |
Correct |
133 ms |
757328 KB |
Output is correct |
9 |
Correct |
135 ms |
757328 KB |
Output is correct |
10 |
Correct |
133 ms |
757516 KB |
Output is correct |
11 |
Correct |
135 ms |
757332 KB |
Output is correct |
12 |
Correct |
133 ms |
757324 KB |
Output is correct |
13 |
Correct |
134 ms |
757524 KB |
Output is correct |
14 |
Correct |
124 ms |
757308 KB |
Output is correct |
15 |
Correct |
126 ms |
757492 KB |
Output is correct |
16 |
Correct |
108 ms |
757524 KB |
Output is correct |
17 |
Correct |
98 ms |
757524 KB |
Output is correct |
18 |
Correct |
101 ms |
757324 KB |
Output is correct |
19 |
Correct |
91 ms |
757484 KB |
Output is correct |
20 |
Correct |
90 ms |
757492 KB |
Output is correct |
21 |
Correct |
90 ms |
757400 KB |
Output is correct |
22 |
Correct |
89 ms |
757376 KB |
Output is correct |
23 |
Correct |
88 ms |
757408 KB |
Output is correct |
24 |
Correct |
86 ms |
757500 KB |
Output is correct |
25 |
Correct |
88 ms |
757328 KB |
Output is correct |
26 |
Correct |
87 ms |
757496 KB |
Output is correct |
27 |
Correct |
87 ms |
757436 KB |
Output is correct |
28 |
Correct |
86 ms |
757392 KB |
Output is correct |
29 |
Correct |
88 ms |
757332 KB |
Output is correct |
30 |
Correct |
87 ms |
757328 KB |
Output is correct |
31 |
Correct |
87 ms |
757432 KB |
Output is correct |
32 |
Correct |
86 ms |
757328 KB |
Output is correct |
33 |
Correct |
86 ms |
757332 KB |
Output is correct |
34 |
Correct |
87 ms |
757524 KB |
Output is correct |
35 |
Correct |
88 ms |
757428 KB |
Output is correct |
36 |
Correct |
88 ms |
757408 KB |
Output is correct |
37 |
Correct |
86 ms |
757332 KB |
Output is correct |
38 |
Correct |
84 ms |
757384 KB |
Output is correct |
39 |
Correct |
88 ms |
757508 KB |
Output is correct |
40 |
Correct |
88 ms |
757552 KB |
Output is correct |
41 |
Correct |
86 ms |
757528 KB |
Output is correct |
42 |
Correct |
86 ms |
757332 KB |
Output is correct |
43 |
Correct |
87 ms |
757372 KB |
Output is correct |
44 |
Correct |
88 ms |
757328 KB |
Output is correct |
45 |
Correct |
88 ms |
757700 KB |
Output is correct |
46 |
Correct |
98 ms |
757428 KB |
Output is correct |
47 |
Correct |
89 ms |
757332 KB |
Output is correct |
48 |
Correct |
87 ms |
757332 KB |
Output is correct |
49 |
Correct |
88 ms |
757332 KB |
Output is correct |
50 |
Correct |
90 ms |
757332 KB |
Output is correct |
51 |
Correct |
86 ms |
757332 KB |
Output is correct |
52 |
Correct |
88 ms |
757524 KB |
Output is correct |
53 |
Correct |
87 ms |
757320 KB |
Output is correct |
54 |
Correct |
87 ms |
757488 KB |
Output is correct |
55 |
Correct |
87 ms |
757312 KB |
Output is correct |
56 |
Correct |
88 ms |
757332 KB |
Output is correct |
57 |
Correct |
90 ms |
757532 KB |
Output is correct |
58 |
Correct |
120 ms |
757332 KB |
Output is correct |
59 |
Correct |
123 ms |
757524 KB |
Output is correct |
60 |
Correct |
122 ms |
757380 KB |
Output is correct |
61 |
Correct |
120 ms |
757332 KB |
Output is correct |
62 |
Correct |
93 ms |
757332 KB |
Output is correct |
63 |
Correct |
96 ms |
757376 KB |
Output is correct |
64 |
Correct |
99 ms |
757328 KB |
Output is correct |
65 |
Correct |
108 ms |
757332 KB |
Output is correct |
66 |
Correct |
119 ms |
757520 KB |
Output is correct |
67 |
Correct |
122 ms |
757676 KB |
Output is correct |
68 |
Correct |
119 ms |
757472 KB |
Output is correct |
69 |
Correct |
119 ms |
757444 KB |
Output is correct |
70 |
Correct |
120 ms |
757328 KB |
Output is correct |
71 |
Correct |
119 ms |
757556 KB |
Output is correct |
72 |
Correct |
91 ms |
757468 KB |
Output is correct |
73 |
Correct |
87 ms |
757332 KB |
Output is correct |