#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]) + cnt[i][j][1][2] + cnt[i][l][1][3];
if(j != 0)dp[i][j][l][2] = min(dp[i][j-1][l][1], dp[i][j-1][l][3]) + cnt[j][i][2][1] + cnt[j][l][2][3];
if(l != 0)dp[i][j][l][3] = min(dp[i][j][l-1][1], dp[i][j][l-1][2]) + cnt[l][j][3][2] + cnt[l][i][3][1];
}
}
}
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();
}
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
468 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 |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
468 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 |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2260 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1748 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
1 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2516 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
1108 KB |
Output is correct |
35 |
Correct |
1 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Correct |
1 ms |
1876 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
5844 KB |
Output is correct |
3 |
Correct |
3 ms |
5844 KB |
Output is correct |
4 |
Correct |
3 ms |
5844 KB |
Output is correct |
5 |
Correct |
3 ms |
5844 KB |
Output is correct |
6 |
Correct |
4 ms |
5844 KB |
Output is correct |
7 |
Correct |
3 ms |
5844 KB |
Output is correct |
8 |
Correct |
3 ms |
5864 KB |
Output is correct |
9 |
Correct |
3 ms |
5804 KB |
Output is correct |
10 |
Correct |
5 ms |
5844 KB |
Output is correct |
11 |
Correct |
3 ms |
5844 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
4436 KB |
Output is correct |
15 |
Correct |
3 ms |
5844 KB |
Output is correct |
16 |
Correct |
3 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
468 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 |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2260 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1748 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
1 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2516 KB |
Output is correct |
30 |
Correct |
1 ms |
2004 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
2 ms |
2004 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
1108 KB |
Output is correct |
35 |
Correct |
1 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Correct |
1 ms |
1876 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
3 ms |
5844 KB |
Output is correct |
44 |
Correct |
3 ms |
5844 KB |
Output is correct |
45 |
Correct |
3 ms |
5844 KB |
Output is correct |
46 |
Correct |
3 ms |
5844 KB |
Output is correct |
47 |
Correct |
4 ms |
5844 KB |
Output is correct |
48 |
Correct |
3 ms |
5844 KB |
Output is correct |
49 |
Correct |
3 ms |
5864 KB |
Output is correct |
50 |
Correct |
3 ms |
5804 KB |
Output is correct |
51 |
Correct |
5 ms |
5844 KB |
Output is correct |
52 |
Correct |
3 ms |
5844 KB |
Output is correct |
53 |
Correct |
2 ms |
2388 KB |
Output is correct |
54 |
Correct |
2 ms |
3412 KB |
Output is correct |
55 |
Correct |
3 ms |
4436 KB |
Output is correct |
56 |
Correct |
3 ms |
5844 KB |
Output is correct |
57 |
Correct |
3 ms |
5844 KB |
Output is correct |
58 |
Correct |
70 ms |
114872 KB |
Output is correct |
59 |
Correct |
75 ms |
109132 KB |
Output is correct |
60 |
Correct |
71 ms |
113996 KB |
Output is correct |
61 |
Correct |
80 ms |
112932 KB |
Output is correct |
62 |
Correct |
10 ms |
11664 KB |
Output is correct |
63 |
Correct |
13 ms |
15652 KB |
Output is correct |
64 |
Correct |
32 ms |
45780 KB |
Output is correct |
65 |
Correct |
39 ms |
62568 KB |
Output is correct |
66 |
Correct |
68 ms |
111716 KB |
Output is correct |
67 |
Correct |
57 ms |
100840 KB |
Output is correct |
68 |
Correct |
76 ms |
111188 KB |
Output is correct |
69 |
Correct |
65 ms |
114776 KB |
Output is correct |
70 |
Correct |
62 ms |
108224 KB |
Output is correct |
71 |
Correct |
76 ms |
121676 KB |
Output is correct |
72 |
Correct |
42 ms |
101400 KB |
Output is correct |
73 |
Correct |
7 ms |
12884 KB |
Output is correct |