#include <bits/stdc++.h>
#define ll int
#define pb push_back
#define task "bus"
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
const ll mod = 1e9+7;
const ll N = 405;
const ll base = 250;
using namespace std;
ll n, m, t, k, T, ans, a[N],tong, b[N], c[N], d[N], dp[N][N][N][4], cnt[N][N][4][4], n1, n2, n3;
string s;
void sol()
{
cin >> n >> s;
for(int i = 0; i < n; i ++)
{
if(s[i] == 'R')a[++n1] = i+1;
else if(s[i] == 'Y')b[++n2] = i+1;
else c[++n3] = i+1;
}
for(int i = 1; i <= n1; i ++)
{
k = 0;
for(int j = 1; j <= n2; j ++)
{
if(b[j] > a[i])++k;
cnt[i][j][1][2] = k;
}
k = 0;
for(int j = 1; j <= n3; j ++)
{
if(c[j] > a[i])++k;
cnt[i][j][1][3] = k;
}
}
for(int i = 1; i <= n2; i ++)
{
k = 0;
for(int j = 1; j <= n1; j ++)
{
if(a[j] > b[i])++k;
cnt[i][j][2][1] = k;
}
k = 0;
for(int j = 1; j <= n3; j ++)
{
if(c[j] > b[i])++k;
cnt[i][j][2][3] = k;
}
}
for(int i = 1; i <= n3; i ++)
{
k = 0;
for(int j = 1; j <= n2; j ++)
{
if(b[j] > c[i])++k;
cnt[i][j][3][2] = k;
}
k = 0;
for(int j = 1; j <= n1; j ++)
{
if(a[j] > c[i])++k;
cnt[i][j][3][1] = k;
}
}
for(int i = 0; i <= n1; i ++)for(int j = 0; j <= n2; j ++)for(int l = 0; l <= n3; l ++)
dp[i][j][l][1] = dp[i][j][l][2] = dp[i][j][l][3] = mod;
dp[0][0][0][1] = dp[0][0][0][2] = dp[0][0][0][3] = 0;
//cout << n1 <<" "<<n2<<" "<<n3<<'\n';
for(int i = 0; i <= n1; i ++)
{
for(int j = 0; j <= n2; j ++)
{
for(int l = 0; l <= n3; l ++)
{
if(i == 0 && j == 0 && l == 0)continue;
if(i != 0)dp[i][j][l][1] = min(dp[i-1][j][l][2], dp[i-1][j][l][3]) + a[i] + cnt[i][j][1][2] + cnt[i][l][1][3] - (i + j + l);
if(j != 0)dp[i][j][l][2] = min(dp[i][j-1][l][1], dp[i][j-1][l][3]) + b[j] + cnt[j][i][2][1] + cnt[j][l][2][3] - (i + j + l);
if(l != 0)dp[i][j][l][3] = min(dp[i][j][l-1][1], dp[i][j][l-1][2]) + c[l] + cnt[l][j][3][2] + cnt[l][i][3][1] - (i + j + l);
}
}
}
ans = min(min(dp[n1][n2][n3][1], dp[n1][n2][n3][2]), dp[n1][n2][n3][3]);
if(ans >= mod)ans = -1;
cout << ans;
}
int main()
{
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest;
ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
1 3
1 -1 1
10 5
487125983 7690599 333427386 884957999 246724760 224342552 130825246 518149612 37072017 132013639
*/
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:94:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
94 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:95:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
95 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
2 ms |
2668 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
2284 KB |
Output is correct |
22 |
Correct |
2 ms |
3308 KB |
Output is correct |
23 |
Correct |
2 ms |
2284 KB |
Output is correct |
24 |
Correct |
1 ms |
1772 KB |
Output is correct |
25 |
Correct |
1 ms |
1004 KB |
Output is correct |
26 |
Correct |
1 ms |
1004 KB |
Output is correct |
27 |
Correct |
1 ms |
2028 KB |
Output is correct |
28 |
Correct |
2 ms |
2412 KB |
Output is correct |
29 |
Correct |
3 ms |
2668 KB |
Output is correct |
30 |
Correct |
2 ms |
2176 KB |
Output is correct |
31 |
Correct |
2 ms |
2156 KB |
Output is correct |
32 |
Correct |
2 ms |
2156 KB |
Output is correct |
33 |
Correct |
1 ms |
1004 KB |
Output is correct |
34 |
Correct |
1 ms |
1260 KB |
Output is correct |
35 |
Correct |
2 ms |
2028 KB |
Output is correct |
36 |
Correct |
2 ms |
2816 KB |
Output is correct |
37 |
Correct |
2 ms |
1900 KB |
Output is correct |
38 |
Correct |
1 ms |
1900 KB |
Output is correct |
39 |
Correct |
2 ms |
2284 KB |
Output is correct |
40 |
Correct |
1 ms |
748 KB |
Output is correct |
41 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
4 ms |
5868 KB |
Output is correct |
3 |
Correct |
4 ms |
5868 KB |
Output is correct |
4 |
Correct |
4 ms |
5868 KB |
Output is correct |
5 |
Correct |
4 ms |
5868 KB |
Output is correct |
6 |
Correct |
6 ms |
5868 KB |
Output is correct |
7 |
Correct |
4 ms |
5868 KB |
Output is correct |
8 |
Correct |
5 ms |
5868 KB |
Output is correct |
9 |
Correct |
5 ms |
5868 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
6 ms |
5868 KB |
Output is correct |
12 |
Correct |
2 ms |
2412 KB |
Output is correct |
13 |
Correct |
2 ms |
3436 KB |
Output is correct |
14 |
Correct |
3 ms |
4460 KB |
Output is correct |
15 |
Correct |
5 ms |
5868 KB |
Output is correct |
16 |
Correct |
5 ms |
5868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
2 ms |
2668 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
2 ms |
2284 KB |
Output is correct |
22 |
Correct |
2 ms |
3308 KB |
Output is correct |
23 |
Correct |
2 ms |
2284 KB |
Output is correct |
24 |
Correct |
1 ms |
1772 KB |
Output is correct |
25 |
Correct |
1 ms |
1004 KB |
Output is correct |
26 |
Correct |
1 ms |
1004 KB |
Output is correct |
27 |
Correct |
1 ms |
2028 KB |
Output is correct |
28 |
Correct |
2 ms |
2412 KB |
Output is correct |
29 |
Correct |
3 ms |
2668 KB |
Output is correct |
30 |
Correct |
2 ms |
2176 KB |
Output is correct |
31 |
Correct |
2 ms |
2156 KB |
Output is correct |
32 |
Correct |
2 ms |
2156 KB |
Output is correct |
33 |
Correct |
1 ms |
1004 KB |
Output is correct |
34 |
Correct |
1 ms |
1260 KB |
Output is correct |
35 |
Correct |
2 ms |
2028 KB |
Output is correct |
36 |
Correct |
2 ms |
2816 KB |
Output is correct |
37 |
Correct |
2 ms |
1900 KB |
Output is correct |
38 |
Correct |
1 ms |
1900 KB |
Output is correct |
39 |
Correct |
2 ms |
2284 KB |
Output is correct |
40 |
Correct |
1 ms |
748 KB |
Output is correct |
41 |
Correct |
1 ms |
1152 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
4 ms |
5868 KB |
Output is correct |
44 |
Correct |
4 ms |
5868 KB |
Output is correct |
45 |
Correct |
4 ms |
5868 KB |
Output is correct |
46 |
Correct |
4 ms |
5868 KB |
Output is correct |
47 |
Correct |
6 ms |
5868 KB |
Output is correct |
48 |
Correct |
4 ms |
5868 KB |
Output is correct |
49 |
Correct |
5 ms |
5868 KB |
Output is correct |
50 |
Correct |
5 ms |
5868 KB |
Output is correct |
51 |
Correct |
5 ms |
5868 KB |
Output is correct |
52 |
Correct |
6 ms |
5868 KB |
Output is correct |
53 |
Correct |
2 ms |
2412 KB |
Output is correct |
54 |
Correct |
2 ms |
3436 KB |
Output is correct |
55 |
Correct |
3 ms |
4460 KB |
Output is correct |
56 |
Correct |
5 ms |
5868 KB |
Output is correct |
57 |
Correct |
5 ms |
5868 KB |
Output is correct |
58 |
Correct |
92 ms |
115052 KB |
Output is correct |
59 |
Correct |
88 ms |
109164 KB |
Output is correct |
60 |
Correct |
102 ms |
114156 KB |
Output is correct |
61 |
Correct |
93 ms |
113004 KB |
Output is correct |
62 |
Correct |
9 ms |
11756 KB |
Output is correct |
63 |
Correct |
13 ms |
15724 KB |
Output is correct |
64 |
Correct |
40 ms |
45804 KB |
Output is correct |
65 |
Correct |
62 ms |
62700 KB |
Output is correct |
66 |
Correct |
90 ms |
111852 KB |
Output is correct |
67 |
Correct |
84 ms |
100972 KB |
Output is correct |
68 |
Correct |
88 ms |
111212 KB |
Output is correct |
69 |
Correct |
92 ms |
114816 KB |
Output is correct |
70 |
Correct |
92 ms |
108396 KB |
Output is correct |
71 |
Correct |
95 ms |
121964 KB |
Output is correct |
72 |
Correct |
59 ms |
101484 KB |
Output is correct |
73 |
Correct |
11 ms |
12908 KB |
Output is correct |