//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];
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 = 1;
for (char &c : s)
{
++cnt[convert(c)];
pos[convert(c)].push_back(i);
++i;
}
}
bool minimize(int &a, int b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
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]) + abs(i + j + k - pos[0][i]));
}
if (j > 0)
{
minimize(dp[i][j][k][1], min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + abs(i + j + k - pos[1][j]));
}
if (k > 0)
{
minimize(dp[i][j][k][2], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + abs(i + j + k - pos[2][k]));
}
}
}
}
int res = 1e9;
for (int i = 0; i < 3; ++ i)
{
res = min(res, dp[cnt[0]][cnt[1]][cnt[2]][i]);
}
cout << res/2;
}
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 main()':
joi2019_ho_t3.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
34 ms |
95604 KB |
Output is correct |
3 |
Correct |
33 ms |
95448 KB |
Output is correct |
4 |
Correct |
44 ms |
95556 KB |
Output is correct |
5 |
Correct |
42 ms |
95564 KB |
Output is correct |
6 |
Correct |
35 ms |
95608 KB |
Output is correct |
7 |
Correct |
37 ms |
95100 KB |
Output is correct |
8 |
Correct |
33 ms |
95072 KB |
Output is correct |
9 |
Correct |
34 ms |
95060 KB |
Output is correct |
10 |
Correct |
35 ms |
95572 KB |
Output is correct |
11 |
Correct |
35 ms |
95620 KB |
Output is correct |
12 |
Correct |
10 ms |
26068 KB |
Output is correct |
13 |
Correct |
20 ms |
45524 KB |
Output is correct |
14 |
Correct |
29 ms |
65568 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |