/**
____ ____ ____ ____ ____ ____
||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];
pair < int, int > prf[3][maxn];
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 i = 0; i < pos[0].size(); i ++)
{
int f1 = 0;
while(f1 < pos[1].size() && pos[0][i] > pos[1][f1])
f1 ++;
int f2 = 0;
while(f2 < pos[2].size() && pos[0][i] > pos[2][f2])
f2 ++;
///cout << i << " " << f1 << " " << f2 << endl;
prf[0][i] = {f1, f2};
}
for (int i = 0; i < pos[1].size(); i ++)
{
int f1 = 0;
while(f1 < pos[0].size() && pos[1][i] > pos[0][f1])
f1 ++;
int f2 = 0;
while(f2 < pos[2].size() && pos[1][i] > pos[2][f2])
f2 ++;
prf[1][i] = {f1, f2};
}
for (int i = 0; i < pos[2].size(); i ++)
{
int f1 = 0;
while(f1 < pos[0].size() && pos[2][i] > pos[0][f1])
f1 ++;
int f2 = 0;
while(f2 < pos[1].size() && pos[2][i] > pos[1][f2])
f2 ++;
prf[2][i] = {f1, f2};
}
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))
+ max(0, j - prf[0][i].first)
+ max(0, k - prf[0][i].second);
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))
+ max(0, i - prf[1][j].first)
+ max(0, k - prf[1][j].second);
/**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)
+ max(0, i - prf[2][k].first)
+ max(0, j - prf[2][k].second);
/**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:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i <= pos[0].size(); i ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int j = 0; j <= pos[1].size(); j ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int k = 0; k <= pos[2].size(); k ++)
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < pos[0].size(); i ++)
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | while(f1 < pos[1].size() && pos[0][i] > pos[1][f1])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(f2 < pos[2].size() && pos[0][i] > pos[2][f2])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < pos[1].size(); i ++)
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | while(f1 < pos[0].size() && pos[1][i] > pos[0][f1])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | while(f2 < pos[2].size() && pos[1][i] > pos[2][f2])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < pos[2].size(); i ++)
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | while(f1 < pos[0].size() && pos[2][i] > pos[0][f1])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:86:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | while(f2 < pos[1].size() && pos[2][i] > pos[1][f2])
| ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | 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 |
440 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
440 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1848 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2356 KB |
Output is correct |
22 |
Correct |
1 ms |
1592 KB |
Output is correct |
23 |
Correct |
2 ms |
2100 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3896 KB |
Output is correct |
26 |
Correct |
2 ms |
4152 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
3 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2352 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2232 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
70 ms |
162900 KB |
Output is correct |
3 |
Correct |
74 ms |
162124 KB |
Output is correct |
4 |
Correct |
71 ms |
162940 KB |
Output is correct |
5 |
Correct |
109 ms |
162876 KB |
Output is correct |
6 |
Correct |
71 ms |
162928 KB |
Output is correct |
7 |
Correct |
78 ms |
162120 KB |
Output is correct |
8 |
Correct |
79 ms |
162112 KB |
Output is correct |
9 |
Correct |
70 ms |
161360 KB |
Output is correct |
10 |
Correct |
77 ms |
162984 KB |
Output is correct |
11 |
Correct |
72 ms |
162920 KB |
Output is correct |
12 |
Correct |
29 ms |
44024 KB |
Output is correct |
13 |
Correct |
37 ms |
77080 KB |
Output is correct |
14 |
Correct |
45 ms |
111240 KB |
Output is correct |
15 |
Correct |
71 ms |
162992 KB |
Output is correct |
16 |
Correct |
74 ms |
162988 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 |
440 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1848 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2356 KB |
Output is correct |
22 |
Correct |
1 ms |
1592 KB |
Output is correct |
23 |
Correct |
2 ms |
2100 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3896 KB |
Output is correct |
26 |
Correct |
2 ms |
4152 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
3 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2352 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2232 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2108 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
70 ms |
162900 KB |
Output is correct |
44 |
Correct |
74 ms |
162124 KB |
Output is correct |
45 |
Correct |
71 ms |
162940 KB |
Output is correct |
46 |
Correct |
109 ms |
162876 KB |
Output is correct |
47 |
Correct |
71 ms |
162928 KB |
Output is correct |
48 |
Correct |
78 ms |
162120 KB |
Output is correct |
49 |
Correct |
79 ms |
162112 KB |
Output is correct |
50 |
Correct |
70 ms |
161360 KB |
Output is correct |
51 |
Correct |
77 ms |
162984 KB |
Output is correct |
52 |
Correct |
72 ms |
162920 KB |
Output is correct |
53 |
Correct |
29 ms |
44024 KB |
Output is correct |
54 |
Correct |
37 ms |
77080 KB |
Output is correct |
55 |
Correct |
45 ms |
111240 KB |
Output is correct |
56 |
Correct |
71 ms |
162992 KB |
Output is correct |
57 |
Correct |
74 ms |
162988 KB |
Output is correct |
58 |
Correct |
62 ms |
77104 KB |
Output is correct |
59 |
Correct |
74 ms |
97772 KB |
Output is correct |
60 |
Correct |
94 ms |
88804 KB |
Output is correct |
61 |
Correct |
73 ms |
85820 KB |
Output is correct |
62 |
Correct |
74 ms |
160744 KB |
Output is correct |
63 |
Correct |
68 ms |
160120 KB |
Output is correct |
64 |
Correct |
83 ms |
150052 KB |
Output is correct |
65 |
Correct |
77 ms |
138444 KB |
Output is correct |
66 |
Correct |
86 ms |
87144 KB |
Output is correct |
67 |
Correct |
67 ms |
83896 KB |
Output is correct |
68 |
Correct |
67 ms |
89668 KB |
Output is correct |
69 |
Correct |
74 ms |
86516 KB |
Output is correct |
70 |
Correct |
67 ms |
93696 KB |
Output is correct |
71 |
Correct |
81 ms |
84984 KB |
Output is correct |
72 |
Correct |
30 ms |
42040 KB |
Output is correct |
73 |
Correct |
3 ms |
2868 KB |
Output is correct |