//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 |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 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 |
1356 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 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 |
212 KB |
Output is correct |
27 |
Correct |
1 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 |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 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 |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
1604 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 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 |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
34 ms |
95560 KB |
Output is correct |
3 |
Correct |
35 ms |
95444 KB |
Output is correct |
4 |
Correct |
40 ms |
95644 KB |
Output is correct |
5 |
Correct |
36 ms |
95612 KB |
Output is correct |
6 |
Correct |
35 ms |
95572 KB |
Output is correct |
7 |
Correct |
35 ms |
95096 KB |
Output is correct |
8 |
Correct |
35 ms |
95104 KB |
Output is correct |
9 |
Correct |
35 ms |
95004 KB |
Output is correct |
10 |
Correct |
35 ms |
95572 KB |
Output is correct |
11 |
Correct |
35 ms |
95676 KB |
Output is correct |
12 |
Correct |
11 ms |
26068 KB |
Output is correct |
13 |
Correct |
20 ms |
45524 KB |
Output is correct |
14 |
Correct |
25 ms |
65620 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
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 |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
332 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 |
1356 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 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 |
212 KB |
Output is correct |
27 |
Correct |
1 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 |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 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 |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
1604 KB |
Output is correct |
36 |
Correct |
1 ms |
1236 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 |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
34 ms |
95560 KB |
Output is correct |
44 |
Correct |
35 ms |
95444 KB |
Output is correct |
45 |
Correct |
40 ms |
95644 KB |
Output is correct |
46 |
Correct |
36 ms |
95612 KB |
Output is correct |
47 |
Correct |
35 ms |
95572 KB |
Output is correct |
48 |
Correct |
35 ms |
95096 KB |
Output is correct |
49 |
Correct |
35 ms |
95104 KB |
Output is correct |
50 |
Correct |
35 ms |
95004 KB |
Output is correct |
51 |
Correct |
35 ms |
95572 KB |
Output is correct |
52 |
Correct |
35 ms |
95676 KB |
Output is correct |
53 |
Correct |
11 ms |
26068 KB |
Output is correct |
54 |
Correct |
20 ms |
45524 KB |
Output is correct |
55 |
Correct |
25 ms |
65620 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
52 ms |
38188 KB |
Output is correct |
59 |
Correct |
47 ms |
48240 KB |
Output is correct |
60 |
Correct |
46 ms |
43960 KB |
Output is correct |
61 |
Correct |
48 ms |
42496 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
43 ms |
92236 KB |
Output is correct |
64 |
Correct |
44 ms |
80528 KB |
Output is correct |
65 |
Correct |
43 ms |
70164 KB |
Output is correct |
66 |
Correct |
44 ms |
43124 KB |
Output is correct |
67 |
Correct |
44 ms |
41480 KB |
Output is correct |
68 |
Correct |
59 ms |
44420 KB |
Output is correct |
69 |
Correct |
46 ms |
42808 KB |
Output is correct |
70 |
Correct |
48 ms |
46300 KB |
Output is correct |
71 |
Correct |
44 ms |
42072 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |