#include<bits/stdc++.h>
/*
#pragma optimize ("g",on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
*/
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e15, N = 1500, mod = 998244353;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int cnt[N][3];
vector<int> pos[3];
main() {
//freopen("triangles.in", "r", stdin);
//freopen("triangles.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
cnt[i][0] = cnt[i - 1][0];
cnt[i][1] = cnt[i - 1][1];
cnt[i][2] = cnt[i - 1][2];
if (s[i - 1] == 'R') {
cnt[i][0]++;
pos[0].pb(i);
}
if (s[i - 1] == 'G') {
cnt[i][1]++;
pos[1].pb(i);
}
if (s[i - 1] == 'Y') {
cnt[i][2]++;
pos[2].pb(i);
}
}
if (cnt[n][0] > cnt[n][1] + cnt[n][2] + 1) cout << -1, E;
if (cnt[n][1] > cnt[n][0] + cnt[n][2] + 1) cout << -1, E;
if (cnt[n][2] > cnt[n][1] + cnt[n][0] + 1) cout << -1, E;
int dp[cnt[n][0] + 1][cnt[n][1] + 1][cnt[n][2] + 1][3];
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= cnt[n][0]; i++) {
for (int j = 0; j <= cnt[n][1]; j++) {
for (int k = 0; k <= cnt[n][2]; k++) {
if (!i && !j && !k) continue;
int len = i + j + k;
if (!i) dp[i][j][k][0] = inf;
else {
int p = pos[0][i - 1];
p += max(0ll, j - cnt[p][1]) + max(0ll, k - cnt[p][2]);
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + p - len;
}
if (!j) dp[i][j][k][1] = inf;
else {
int p = pos[1][j - 1];
p += max(0ll, i - cnt[p][0]) + max(0ll, k - cnt[p][2]);
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + p - len;
}
if (!k) dp[i][j][k][2] = inf;
else {
int p = pos[2][k - 1];
p += max(0ll, j - cnt[p][1]) + max(0ll, i - cnt[p][0]);
dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + p - len;
}
}
}
}
cout << min(dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][0], min(dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][1], dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][2]));
}
Compilation message
joi2019_ho_t3.cpp:39:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
39 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
0 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
0 ms |
460 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
844 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
0 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
0 ms |
460 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
1228 KB |
Output is correct |
44 |
Correct |
1 ms |
1228 KB |
Output is correct |
45 |
Correct |
1 ms |
1228 KB |
Output is correct |
46 |
Correct |
1 ms |
1228 KB |
Output is correct |
47 |
Correct |
1 ms |
1228 KB |
Output is correct |
48 |
Correct |
1 ms |
1228 KB |
Output is correct |
49 |
Correct |
1 ms |
1228 KB |
Output is correct |
50 |
Correct |
2 ms |
1228 KB |
Output is correct |
51 |
Correct |
2 ms |
1228 KB |
Output is correct |
52 |
Correct |
1 ms |
1228 KB |
Output is correct |
53 |
Correct |
0 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
716 KB |
Output is correct |
55 |
Correct |
1 ms |
844 KB |
Output is correct |
56 |
Correct |
0 ms |
204 KB |
Output is correct |
57 |
Correct |
0 ms |
204 KB |
Output is correct |
58 |
Correct |
44 ms |
56524 KB |
Output is correct |
59 |
Correct |
37 ms |
56588 KB |
Output is correct |
60 |
Correct |
38 ms |
56208 KB |
Output is correct |
61 |
Correct |
38 ms |
56780 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
7 ms |
8524 KB |
Output is correct |
64 |
Correct |
22 ms |
28140 KB |
Output is correct |
65 |
Correct |
32 ms |
40400 KB |
Output is correct |
66 |
Correct |
38 ms |
55948 KB |
Output is correct |
67 |
Correct |
38 ms |
55888 KB |
Output is correct |
68 |
Correct |
37 ms |
56688 KB |
Output is correct |
69 |
Correct |
49 ms |
57188 KB |
Output is correct |
70 |
Correct |
40 ms |
57008 KB |
Output is correct |
71 |
Correct |
41 ms |
55692 KB |
Output is correct |
72 |
Correct |
0 ms |
332 KB |
Output is correct |
73 |
Correct |
0 ms |
204 KB |
Output is correct |