//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 200;
vector<int> pos[3];
int cnt[3], dp[N + 1][N + 1][N + 1][3], f[2 * N + 1][3];
int n;
string s;
int convert(char c)
{
if (c == 'R')
{
return 0;
}
if (c == 'G')
{
return 1;
}
if (c == 'Y')
{
return 2;
}
}
void read()
{
pos[0] = pos[1] = pos[2] = {0};
cin >> n >> s;
int i = 0;
for (char &c : s)
{
++i;
++cnt[convert(c)];
for (int j = 0; j < 3; ++ j)
{
f[i][j] = f[i - 1][j];
}
++f[i][convert(c)];
pos[convert(c)].push_back(i);
}
}
bool minimize(int &a, int b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
int dis(int i, int j, int k, int t)
{
if (t == 0)
{
return pos[0][i] - i - f[min(pos[0][i], pos[1][j])][1] - f[min(pos[0][i], pos[2][k])][2];
}
if (t == 1)
{
return pos[1][j] - j - f[min(pos[1][j], pos[0][i])][0] - f[min(pos[1][j], pos[2][k])][2];
}
if (t == 2)
{
return pos[2][k] - k - f[min(pos[2][k], pos[0][i])][0] - f[min(pos[2][k], pos[1][j])][1];
}
}
void solve()
{
if (max({cnt[0], cnt[1], cnt[2]}) > (n + 1)/2)
{
cout << -1;
return ;
}
for (int i = 0; i <= cnt[0]; ++ i)
{
for (int j = 0; j <= cnt[1]; ++ j)
{
for (int k = 0; k <= cnt[2]; ++ k)
{
for (int l = 0; l < 3; ++ l)
{
if (i == 0 && j == 0 && k == 0)
{
dp[i][j][k][l] = 0;
}
else
{
dp[i][j][k][l] = 1e9;
}
}
if (i > 0)
{
minimize(dp[i][j][k][0], min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + dis(i, j, k, 0));
}
if (j > 0)
{
minimize(dp[i][j][k][1], min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + dis(i, j, k, 1));
}
if (k > 0)
{
minimize(dp[i][j][k][2], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + dis(i, j, k, 2));
}
//cerr << i << ' ' << j << ' ' << k << ' ' << dp[i][j][k][0] << ' ' << dp[i][j][k][1] << ' ' << dp[i][j][k][2] << '\n';
}
}
}
int res = 1e9;
for (int i = 0; i < 3; ++ i)
{
res = min(res, dp[cnt[0]][cnt[1]][cnt[2]][i]);
}
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int convert(char)':
joi2019_ho_t3.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
26 | }
| ^
joi2019_ho_t3.cpp: In function 'int dis(int, int, int, int)':
joi2019_ho_t3.cpp:67:1: warning: control reaches end of non-void function [-Wreturn-type]
67 | }
| ^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
2 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1096 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1352 KB |
Output is correct |
31 |
Correct |
1 ms |
1224 KB |
Output is correct |
32 |
Correct |
1 ms |
1492 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
2 ms |
1620 KB |
Output is correct |
36 |
Correct |
2 ms |
1224 KB |
Output is correct |
37 |
Correct |
1 ms |
1108 KB |
Output is correct |
38 |
Correct |
1 ms |
1236 KB |
Output is correct |
39 |
Correct |
1 ms |
1364 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
48 ms |
95564 KB |
Output is correct |
3 |
Correct |
39 ms |
95436 KB |
Output is correct |
4 |
Correct |
38 ms |
95552 KB |
Output is correct |
5 |
Correct |
39 ms |
95564 KB |
Output is correct |
6 |
Correct |
40 ms |
95656 KB |
Output is correct |
7 |
Correct |
38 ms |
95184 KB |
Output is correct |
8 |
Correct |
41 ms |
95060 KB |
Output is correct |
9 |
Correct |
50 ms |
95020 KB |
Output is correct |
10 |
Correct |
39 ms |
95564 KB |
Output is correct |
11 |
Correct |
39 ms |
95560 KB |
Output is correct |
12 |
Correct |
11 ms |
26076 KB |
Output is correct |
13 |
Correct |
19 ms |
45416 KB |
Output is correct |
14 |
Correct |
34 ms |
65544 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1236 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
2 ms |
1492 KB |
Output is correct |
22 |
Correct |
2 ms |
1096 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
1876 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1352 KB |
Output is correct |
31 |
Correct |
1 ms |
1224 KB |
Output is correct |
32 |
Correct |
1 ms |
1492 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
2 ms |
1620 KB |
Output is correct |
36 |
Correct |
2 ms |
1224 KB |
Output is correct |
37 |
Correct |
1 ms |
1108 KB |
Output is correct |
38 |
Correct |
1 ms |
1236 KB |
Output is correct |
39 |
Correct |
1 ms |
1364 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
48 ms |
95564 KB |
Output is correct |
44 |
Correct |
39 ms |
95436 KB |
Output is correct |
45 |
Correct |
38 ms |
95552 KB |
Output is correct |
46 |
Correct |
39 ms |
95564 KB |
Output is correct |
47 |
Correct |
40 ms |
95656 KB |
Output is correct |
48 |
Correct |
38 ms |
95184 KB |
Output is correct |
49 |
Correct |
41 ms |
95060 KB |
Output is correct |
50 |
Correct |
50 ms |
95020 KB |
Output is correct |
51 |
Correct |
39 ms |
95564 KB |
Output is correct |
52 |
Correct |
39 ms |
95560 KB |
Output is correct |
53 |
Correct |
11 ms |
26076 KB |
Output is correct |
54 |
Correct |
19 ms |
45416 KB |
Output is correct |
55 |
Correct |
34 ms |
65544 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
328 KB |
Output is correct |
58 |
Correct |
46 ms |
38220 KB |
Output is correct |
59 |
Correct |
51 ms |
48248 KB |
Output is correct |
60 |
Correct |
55 ms |
43956 KB |
Output is correct |
61 |
Correct |
50 ms |
42444 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
47 ms |
92272 KB |
Output is correct |
64 |
Correct |
52 ms |
80552 KB |
Output is correct |
65 |
Correct |
59 ms |
70124 KB |
Output is correct |
66 |
Correct |
46 ms |
43088 KB |
Output is correct |
67 |
Correct |
61 ms |
41424 KB |
Output is correct |
68 |
Correct |
65 ms |
44432 KB |
Output is correct |
69 |
Correct |
60 ms |
42756 KB |
Output is correct |
70 |
Correct |
67 ms |
46272 KB |
Output is correct |
71 |
Correct |
72 ms |
42096 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |