//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;
#define debug(s) cout << #s << " = " << s << endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a, val) memset(a, val, sizeof (a))
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream &os, const pair<F, S> &p) {
return os << "(" << p.first << ", " << p.second << ")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
std::priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
const int maxn = 407;
const int inf = 1e8;
int where[3][maxn >> 1][maxn >> 1][maxn >> 1];
int a[maxn], cnt[5], n;
vector<int> g[5];
int dp[3][maxn >> 1][maxn >> 1][maxn >> 1];
int p[3][maxn];
int32_t main () {
// freopen("in", "r", stdin);
char str[maxn];
scanf("%d", &n);
scanf("%s", str + 1);
for(int i = 1; i <= n; i++) {
if(str[i] == 'R') g[0].emplace_back(i);
else if(str[i] == 'G') g[1].emplace_back(i);
else g[2].emplace_back(i);
}
for(int i = 0; i < 3; i++) {
for(int j : g[i]) a[j] = i, p[i][j] = 1;
cnt[i] = g[i].size();
for(int j = 1; j <= n; j++) p[i][j] += p[i][j - 1];
}
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
puts("-1"); exit(0);
}
for(int i = 0; i <= cnt[0]; i++) {
for(int j = 0; j <= cnt[1]; j++) {
for(int k = 0; k <= cnt[2]; k++) {
if(i != 0) where[0][i][j][k] = (j ? min(j, p[1][g[0][i - 1] - 1]) : 0) + (k ? min(k, p[2][g[0][i - 1] - 1]) : 0) + i;
if(j != 0) where[1][i][j][k] = (i ? min(i, p[0][g[1][j - 1] - 1]) : 0) + (k ? min(k, p[2][g[1][j - 1] - 1]) : 0) + j;
if(k != 0) where[2][i][j][k] = (j ? min(j, p[1][g[2][k - 1] - 1]) : 0) + (i ? min(i, p[0][g[2][k - 1] - 1]) : 0) + k;
}
}
}
for(int i = 0; i <= cnt[0]; i++) {
for(int j = 0; j <= cnt[1]; j++) {
for(int k = 0; k <= cnt[2]; k++) if(i != 0 or j != 0 or k != 0) {
for(int l = 0; l < 3; l++) {
dp[l][i][j][k] = inf;
if(l != 0 and i > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[0][i][j][k] + dp[0][i - 1][j][k]);
if(l != 1 and j > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[1][i][j][k] + dp[1][i][j - 1][k]);
if(l != 2 and k > 0) dp[l][i][j][k] = min(dp[l][i][j][k], i + j + k - where[2][i][j][k] + dp[2][i][j][k - 1]);
}
}
}
}
int ans = inf;
for(int i = 0; i < 3; i++) {
ans = min(ans, dp[i][cnt[0]][cnt[1]][cnt[2]]);
}
printf("%d\n", ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int myrand(int, int)':
joi2019_ho_t3.cpp:18:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
joi2019_ho_t3.cpp:18:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:58:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < 3; i++) if(g[i].size() > (n - 1) / 2 + 1) {
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str + 1);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
17 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
17 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
3072 KB |
Output is correct |
19 |
Correct |
4 ms |
2560 KB |
Output is correct |
20 |
Correct |
4 ms |
2944 KB |
Output is correct |
21 |
Correct |
5 ms |
3072 KB |
Output is correct |
22 |
Correct |
4 ms |
2304 KB |
Output is correct |
23 |
Correct |
5 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2048 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
3968 KB |
Output is correct |
28 |
Correct |
4 ms |
2944 KB |
Output is correct |
29 |
Correct |
5 ms |
2944 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
4 ms |
2560 KB |
Output is correct |
32 |
Correct |
4 ms |
3072 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
3456 KB |
Output is correct |
36 |
Correct |
4 ms |
2560 KB |
Output is correct |
37 |
Correct |
4 ms |
2176 KB |
Output is correct |
38 |
Correct |
4 ms |
2688 KB |
Output is correct |
39 |
Correct |
4 ms |
2944 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
116 ms |
162296 KB |
Output is correct |
3 |
Correct |
113 ms |
162296 KB |
Output is correct |
4 |
Correct |
141 ms |
162296 KB |
Output is correct |
5 |
Correct |
116 ms |
162260 KB |
Output is correct |
6 |
Correct |
114 ms |
162348 KB |
Output is correct |
7 |
Correct |
132 ms |
161400 KB |
Output is correct |
8 |
Correct |
119 ms |
161400 KB |
Output is correct |
9 |
Correct |
119 ms |
161528 KB |
Output is correct |
10 |
Correct |
114 ms |
162232 KB |
Output is correct |
11 |
Correct |
134 ms |
162296 KB |
Output is correct |
12 |
Correct |
36 ms |
44920 KB |
Output is correct |
13 |
Correct |
57 ms |
78004 KB |
Output is correct |
14 |
Correct |
87 ms |
112248 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
768 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
17 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
3072 KB |
Output is correct |
19 |
Correct |
4 ms |
2560 KB |
Output is correct |
20 |
Correct |
4 ms |
2944 KB |
Output is correct |
21 |
Correct |
5 ms |
3072 KB |
Output is correct |
22 |
Correct |
4 ms |
2304 KB |
Output is correct |
23 |
Correct |
5 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2048 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
3968 KB |
Output is correct |
28 |
Correct |
4 ms |
2944 KB |
Output is correct |
29 |
Correct |
5 ms |
2944 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
4 ms |
2560 KB |
Output is correct |
32 |
Correct |
4 ms |
3072 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
3456 KB |
Output is correct |
36 |
Correct |
4 ms |
2560 KB |
Output is correct |
37 |
Correct |
4 ms |
2176 KB |
Output is correct |
38 |
Correct |
4 ms |
2688 KB |
Output is correct |
39 |
Correct |
4 ms |
2944 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
116 ms |
162296 KB |
Output is correct |
44 |
Correct |
113 ms |
162296 KB |
Output is correct |
45 |
Correct |
141 ms |
162296 KB |
Output is correct |
46 |
Correct |
116 ms |
162260 KB |
Output is correct |
47 |
Correct |
114 ms |
162348 KB |
Output is correct |
48 |
Correct |
132 ms |
161400 KB |
Output is correct |
49 |
Correct |
119 ms |
161400 KB |
Output is correct |
50 |
Correct |
119 ms |
161528 KB |
Output is correct |
51 |
Correct |
114 ms |
162232 KB |
Output is correct |
52 |
Correct |
134 ms |
162296 KB |
Output is correct |
53 |
Correct |
36 ms |
44920 KB |
Output is correct |
54 |
Correct |
57 ms |
78004 KB |
Output is correct |
55 |
Correct |
87 ms |
112248 KB |
Output is correct |
56 |
Correct |
2 ms |
384 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
112 ms |
78896 KB |
Output is correct |
59 |
Correct |
126 ms |
99524 KB |
Output is correct |
60 |
Correct |
113 ms |
90616 KB |
Output is correct |
61 |
Correct |
114 ms |
87544 KB |
Output is correct |
62 |
Correct |
2 ms |
384 KB |
Output is correct |
63 |
Correct |
142 ms |
188792 KB |
Output is correct |
64 |
Correct |
150 ms |
165368 KB |
Output is correct |
65 |
Correct |
159 ms |
143840 KB |
Output is correct |
66 |
Correct |
112 ms |
88824 KB |
Output is correct |
67 |
Correct |
112 ms |
85112 KB |
Output is correct |
68 |
Correct |
119 ms |
91384 KB |
Output is correct |
69 |
Correct |
119 ms |
88240 KB |
Output is correct |
70 |
Correct |
134 ms |
95352 KB |
Output is correct |
71 |
Correct |
112 ms |
87032 KB |
Output is correct |
72 |
Correct |
2 ms |
384 KB |
Output is correct |
73 |
Correct |
2 ms |
384 KB |
Output is correct |