/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 410, inf = 1e9;
int n, a[maxn], dp[maxn][maxn][maxn][3];
string s;
vector < int > pos[3];
void solve()
{
cin >> n >> s;
for (int i = 0; i < n; i ++)
{
if (s[i] == 'R')
a[i + 1] = 0;
else if (s[i] == 'G')
a[i + 1] = 1;
else if (s[i] == 'Y')
a[i + 1] = 2;
}
for (int i = 1; i <= n; i ++)
pos[a[i]].push_back(i);
for (int i = 0; i <= pos[0].size(); i ++)
for (int j = 0; j <= pos[1].size(); j ++)
for (int k = 0; k <= pos[2].size(); k ++)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = inf;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int p = 0; p < n; p ++)
{
for (int i = 0; i <= min(p, (int)pos[0].size()); i ++)
for (int j = 0; j <= min(p - i, (int)(pos[1].size())); j ++)
{
int k = p - i - j;
if (k > pos[2].size())
continue;
///cout << i << " " << j << " " << k << " " << dp[i][j][k][0] << " " << dp[i][j][k][1] << " " << dp[i][j][k][2] << endl;
if (i < (int)pos[0].size())
{
int add0 = min(dp[i][j][k][1], dp[i][j][k][2]);
int sd = (pos[0][i] - (p + 1));
for (int f = 0; f < j; f ++)
if (pos[1][f] > pos[0][i])
sd ++;
for (int f = 0; f < k; f ++)
if (pos[2][f] > pos[0][i])
sd ++;
add0 = add0 + max(0, sd);
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], add0);
}
if (j < (int)pos[1].size())
{
///cout << "here" << endl;
int add1 = min(dp[i][j][k][0], dp[i][j][k][2]);
int sd = (pos[1][j] - (p + 1));
for (int f = 0; f < i; f ++)
if (pos[0][f] > pos[1][j])
sd ++;
for (int f = 0; f < k; f ++)
if (pos[2][f] > pos[1][j])
sd ++;
add1 = add1 + max(0, sd);
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], add1);
}
if (k < (int)pos[2].size())
{
int add2 = min(dp[i][j][k][0], dp[i][j][k][1]);
int sd = pos[2][k] - (p + 1);
for (int f = 0; f < i; f ++)
if (pos[0][f] > pos[2][k])
sd ++;
for (int f = 0; f < j; f ++)
if (pos[1][f] > pos[2][k])
sd ++;
add2 = add2 + max(0, sd);
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], add2);
}
}
}
///cout << dp[1][1][0][1] << endl;
int ans = inf;
for (int last = 0; last < 3; last ++)
ans = min(ans, dp[pos[0].size()][pos[1].size()][pos[2].size()][last]);
if (ans >= inf)
cout << -1 << endl;
else
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
/**
15
YYYRRRRRGGGGGGG
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i <= pos[0].size(); i ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int j = 0; j <= pos[1].size(); j ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int k = 0; k <= pos[2].size(); k ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (k > pos[2].size())
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
2260 KB |
Output is correct |
19 |
Correct |
3 ms |
1876 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
2 ms |
1588 KB |
Output is correct |
23 |
Correct |
2 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2228 KB |
Output is correct |
30 |
Correct |
2 ms |
2232 KB |
Output is correct |
31 |
Correct |
2 ms |
1876 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
3 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
3 ms |
1724 KB |
Output is correct |
38 |
Correct |
3 ms |
2004 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
72 ms |
162876 KB |
Output is correct |
3 |
Correct |
71 ms |
162152 KB |
Output is correct |
4 |
Correct |
77 ms |
163020 KB |
Output is correct |
5 |
Correct |
71 ms |
162860 KB |
Output is correct |
6 |
Correct |
73 ms |
162972 KB |
Output is correct |
7 |
Correct |
71 ms |
162096 KB |
Output is correct |
8 |
Correct |
72 ms |
162160 KB |
Output is correct |
9 |
Correct |
71 ms |
161256 KB |
Output is correct |
10 |
Correct |
74 ms |
162976 KB |
Output is correct |
11 |
Correct |
78 ms |
162920 KB |
Output is correct |
12 |
Correct |
24 ms |
44016 KB |
Output is correct |
13 |
Correct |
36 ms |
77132 KB |
Output is correct |
14 |
Correct |
48 ms |
111316 KB |
Output is correct |
15 |
Correct |
73 ms |
162964 KB |
Output is correct |
16 |
Correct |
72 ms |
162964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
392 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
2260 KB |
Output is correct |
19 |
Correct |
3 ms |
1876 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
2 ms |
1588 KB |
Output is correct |
23 |
Correct |
2 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2228 KB |
Output is correct |
30 |
Correct |
2 ms |
2232 KB |
Output is correct |
31 |
Correct |
2 ms |
1876 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
3 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
3 ms |
1724 KB |
Output is correct |
38 |
Correct |
3 ms |
2004 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
72 ms |
162876 KB |
Output is correct |
44 |
Correct |
71 ms |
162152 KB |
Output is correct |
45 |
Correct |
77 ms |
163020 KB |
Output is correct |
46 |
Correct |
71 ms |
162860 KB |
Output is correct |
47 |
Correct |
73 ms |
162972 KB |
Output is correct |
48 |
Correct |
71 ms |
162096 KB |
Output is correct |
49 |
Correct |
72 ms |
162160 KB |
Output is correct |
50 |
Correct |
71 ms |
161256 KB |
Output is correct |
51 |
Correct |
74 ms |
162976 KB |
Output is correct |
52 |
Correct |
78 ms |
162920 KB |
Output is correct |
53 |
Correct |
24 ms |
44016 KB |
Output is correct |
54 |
Correct |
36 ms |
77132 KB |
Output is correct |
55 |
Correct |
48 ms |
111316 KB |
Output is correct |
56 |
Correct |
73 ms |
162964 KB |
Output is correct |
57 |
Correct |
72 ms |
162964 KB |
Output is correct |
58 |
Execution timed out |
833 ms |
77140 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |