/**
____ ____ ____ ____ ____ ____
||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 = 510;
int n, k;
string s;
int dp[maxn][maxn][maxn], pos[2][maxn];
/**
*/
void chmin(int &var, int val) /// careful for long long
{
var = min(var, val);
}
void solve()
{
cin >> n >> k >> s;
s = "#" + s;
int cntA = 0, cntB = 0;
for (int i = 1; i <= 2 * n; i ++)
{
if (s[i] == 'A')
{
cntA ++;
pos[0][cntA] = i;
}
else
{
cntB ++;
pos[1][cntB] = i;
}
}
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= n; j ++)
for (int k = 0; k <= n; k ++)
dp[i][j][k] = 4 * n * n; /// careful for overflow
int cr = 0;
for (int j = 1; j <= n; j ++)
{
cr = cr + pos[0][j] - j;
dp[1][j][j] = cr;
}
for (int i = 1; i < k; i ++)
{
for (int j = 1; j <= n; j ++)
for (int k = 1; k <= j; k ++)
{
if (dp[i][j][k] == 4 * n * n)
continue;
/// i-th song, j open brackets, k - in the last one
/// range [j * 2 - k + 1, j * 2]
///if (dp[i][j][k] == 0)
///cout << "state " << i << " " << j << " " << k << " = " << dp[i][j][k] << endl;
int tk = 0, atl = j * 2 - k + 1, atm = j * 2 + 1;
for (int fix = 1; fix <= n - j; fix ++)
{
///if (pos[0][fix + j] < atl)
// tk += atl - pos[0][fix + j];
///else
if (pos[0][fix + j] > atm)
tk += pos[0][fix + j] - atm;
//cout << "transition " << fix << " " << tk << endl;
///cout << dp[i][j][k] + tk << " " << tk << endl;
chmin(dp[i + 1][j + fix][fix], dp[i][j][k] + tk);
atl ++;
atm ++;
}
}
}
int ans = 4 * n * n;
for (int i = 1; i <= k; i ++)
for (int j = 1; j <= n; j ++)
ans = min(ans, dp[i][n][j]);
cout << ans << endl;
}
int main()
{
///freopen("test.txt", "r", stdin);
solve();
return 0;
}
Compilation message
chorus.cpp: In function 'void solve()':
chorus.cpp:56:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
56 | for (int i = 0; i <= n; i ++)
| ^~~
chorus.cpp:61:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
61 | int cr = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6612 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6612 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
73 ms |
183132 KB |
Output is correct |
18 |
Correct |
2152 ms |
511536 KB |
Output is correct |
19 |
Correct |
3671 ms |
511572 KB |
Output is correct |
20 |
Correct |
138 ms |
511476 KB |
Output is correct |
21 |
Correct |
141 ms |
511312 KB |
Output is correct |
22 |
Correct |
3866 ms |
511780 KB |
Output is correct |
23 |
Correct |
3789 ms |
511540 KB |
Output is correct |
24 |
Correct |
353 ms |
511316 KB |
Output is correct |
25 |
Correct |
3821 ms |
511540 KB |
Output is correct |
26 |
Correct |
3804 ms |
511540 KB |
Output is correct |
27 |
Correct |
3023 ms |
509520 KB |
Output is correct |
28 |
Correct |
3030 ms |
509828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6612 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
73 ms |
183132 KB |
Output is correct |
18 |
Correct |
2152 ms |
511536 KB |
Output is correct |
19 |
Correct |
3671 ms |
511572 KB |
Output is correct |
20 |
Correct |
138 ms |
511476 KB |
Output is correct |
21 |
Correct |
141 ms |
511312 KB |
Output is correct |
22 |
Correct |
3866 ms |
511780 KB |
Output is correct |
23 |
Correct |
3789 ms |
511540 KB |
Output is correct |
24 |
Correct |
353 ms |
511316 KB |
Output is correct |
25 |
Correct |
3821 ms |
511540 KB |
Output is correct |
26 |
Correct |
3804 ms |
511540 KB |
Output is correct |
27 |
Correct |
3023 ms |
509520 KB |
Output is correct |
28 |
Correct |
3030 ms |
509828 KB |
Output is correct |
29 |
Execution timed out |
7002 ms |
480896 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6612 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
73 ms |
183132 KB |
Output is correct |
18 |
Correct |
2152 ms |
511536 KB |
Output is correct |
19 |
Correct |
3671 ms |
511572 KB |
Output is correct |
20 |
Correct |
138 ms |
511476 KB |
Output is correct |
21 |
Correct |
141 ms |
511312 KB |
Output is correct |
22 |
Correct |
3866 ms |
511780 KB |
Output is correct |
23 |
Correct |
3789 ms |
511540 KB |
Output is correct |
24 |
Correct |
353 ms |
511316 KB |
Output is correct |
25 |
Correct |
3821 ms |
511540 KB |
Output is correct |
26 |
Correct |
3804 ms |
511540 KB |
Output is correct |
27 |
Correct |
3023 ms |
509520 KB |
Output is correct |
28 |
Correct |
3030 ms |
509828 KB |
Output is correct |
29 |
Execution timed out |
7002 ms |
480896 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6612 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
73 ms |
183132 KB |
Output is correct |
18 |
Correct |
2152 ms |
511536 KB |
Output is correct |
19 |
Correct |
3671 ms |
511572 KB |
Output is correct |
20 |
Correct |
138 ms |
511476 KB |
Output is correct |
21 |
Correct |
141 ms |
511312 KB |
Output is correct |
22 |
Correct |
3866 ms |
511780 KB |
Output is correct |
23 |
Correct |
3789 ms |
511540 KB |
Output is correct |
24 |
Correct |
353 ms |
511316 KB |
Output is correct |
25 |
Correct |
3821 ms |
511540 KB |
Output is correct |
26 |
Correct |
3804 ms |
511540 KB |
Output is correct |
27 |
Correct |
3023 ms |
509520 KB |
Output is correct |
28 |
Correct |
3030 ms |
509828 KB |
Output is correct |
29 |
Execution timed out |
7002 ms |
480896 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |