#include <bits/stdc++.h>
#define task "V"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 402, INF = 1e9;
int n, dp[2][3][N][N], pre[3][N];
string s;
map<char, int> mp;
vi pos[3];
void upd(int &x, int y) {x = min(x, y);}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> s;
s = '0' + s;
mp['R'] = 0;
mp['G'] = 1;
mp['Y'] = 2;
pos[0].eb(0);
pos[1].eb(0);
pos[2].eb(0);
rep(i, 1, n)
{
rep(j, 0, 2) pre[j][i] = pre[j][i - 1];
pre[mp[s[i]]][i]++;
pos[mp[s[i]]].eb(i);
}
rep(i, 0, pre[0][n]) rep(j, 0, pre[1][n]) rep(t, 0, 2) dp[0][t][i][j] = INF;
rep(t, 0, 2) dp[0][t][0][0] = 0;
rep(i, 1, n)
{
int now = i & 1, lst = now ^ 1;
rep(i1, 0, pre[0][n]) rep(j, 0, pre[1][n]) rep(t, 0, 2) dp[now][t][i1][j] = INF;
rep(num0, 0, min(pre[0][n], (i + 1) >> 1)) rep(num1, 0, min({pre[1][n], i - num0, (i + 1) >> 1}))
{
int num2 = i - num0 - num1;
if (num2 > min(pre[2][n], (i + 1) >> 1)) continue;
if (num0)
{
int cost0 = max(0, pre[1][pos[0][num0]] - num1) + max(0, pre[2][pos[0][num0]] - num2);
upd(dp[now][0][num0][num1], dp[lst][1][num0 - 1][num1] + cost0);
upd(dp[now][0][num0][num1], dp[lst][2][num0 - 1][num1] + cost0);
//cerr << "dp[" << i << "][0][" << num0 << "][" << num1 << "] = " << dp[now][0][num0][num1] << '\n';
}
if (num1)
{
int cost1 = max(0, pre[0][pos[1][num1]] - num0) + max(0, pre[2][pos[1][num1]] - num2);
upd(dp[now][1][num0][num1], dp[lst][0][num0][num1 - 1] + cost1);
upd(dp[now][1][num0][num1], dp[lst][2][num0][num1 - 1] + cost1);
//cerr << "dp[" << i << "][1][" << num0 << "][" << num1 << "] = " << dp[now][1][num0][num1] << '\n';
}
if (num2)
{
int cost2 = max(0, pre[1][pos[2][num2]] - num1) + max(0, pre[0][pos[2][num2]] - num0);
upd(dp[now][2][num0][num1], dp[lst][1][num0][num1] + cost2);
upd(dp[now][2][num0][num1], dp[lst][0][num0][num1] + cost2);
//cerr << "dp[" << i << "][2][" << num0 << "][" << num1 << "] = " << dp[now][2][num0][num1] << '\n';
}
}
}
int ans = 1e9;
rep(j, 0, 2) ans = min(ans, dp[n & 1][j][pre[0][n]][pre[1][n]]);
cout << (ans == 1e9 ? -1 : ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
672 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
640 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
35 ms |
2304 KB |
Output is correct |
3 |
Correct |
34 ms |
2304 KB |
Output is correct |
4 |
Correct |
34 ms |
2304 KB |
Output is correct |
5 |
Correct |
34 ms |
2304 KB |
Output is correct |
6 |
Correct |
35 ms |
2304 KB |
Output is correct |
7 |
Correct |
34 ms |
2304 KB |
Output is correct |
8 |
Correct |
34 ms |
2300 KB |
Output is correct |
9 |
Correct |
34 ms |
2296 KB |
Output is correct |
10 |
Correct |
34 ms |
2304 KB |
Output is correct |
11 |
Correct |
34 ms |
2304 KB |
Output is correct |
12 |
Correct |
6 ms |
1408 KB |
Output is correct |
13 |
Correct |
13 ms |
1664 KB |
Output is correct |
14 |
Correct |
20 ms |
1920 KB |
Output is correct |
15 |
Correct |
34 ms |
2304 KB |
Output is correct |
16 |
Correct |
34 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
544 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
672 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
512 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
512 KB |
Output is correct |
35 |
Correct |
1 ms |
640 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
35 ms |
2304 KB |
Output is correct |
44 |
Correct |
34 ms |
2304 KB |
Output is correct |
45 |
Correct |
34 ms |
2304 KB |
Output is correct |
46 |
Correct |
34 ms |
2304 KB |
Output is correct |
47 |
Correct |
35 ms |
2304 KB |
Output is correct |
48 |
Correct |
34 ms |
2304 KB |
Output is correct |
49 |
Correct |
34 ms |
2300 KB |
Output is correct |
50 |
Correct |
34 ms |
2296 KB |
Output is correct |
51 |
Correct |
34 ms |
2304 KB |
Output is correct |
52 |
Correct |
34 ms |
2304 KB |
Output is correct |
53 |
Correct |
6 ms |
1408 KB |
Output is correct |
54 |
Correct |
13 ms |
1664 KB |
Output is correct |
55 |
Correct |
20 ms |
1920 KB |
Output is correct |
56 |
Correct |
34 ms |
2304 KB |
Output is correct |
57 |
Correct |
34 ms |
2304 KB |
Output is correct |
58 |
Correct |
29 ms |
1656 KB |
Output is correct |
59 |
Correct |
33 ms |
1792 KB |
Output is correct |
60 |
Correct |
31 ms |
1784 KB |
Output is correct |
61 |
Correct |
31 ms |
1664 KB |
Output is correct |
62 |
Correct |
34 ms |
2432 KB |
Output is correct |
63 |
Correct |
34 ms |
2176 KB |
Output is correct |
64 |
Correct |
34 ms |
2176 KB |
Output is correct |
65 |
Correct |
32 ms |
1920 KB |
Output is correct |
66 |
Correct |
31 ms |
1664 KB |
Output is correct |
67 |
Correct |
31 ms |
1408 KB |
Output is correct |
68 |
Correct |
31 ms |
1664 KB |
Output is correct |
69 |
Correct |
33 ms |
1784 KB |
Output is correct |
70 |
Correct |
33 ms |
1664 KB |
Output is correct |
71 |
Correct |
30 ms |
1792 KB |
Output is correct |
72 |
Correct |
12 ms |
3200 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |