# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
906478 |
2024-01-14T10:48:14 Z |
LucaIlie |
Chorus (JOI23_chorus) |
C++17 |
|
7000 ms |
13492 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6;
const long long INF = 1e18;
int a[MAX_N + 1], countLowerA[MAX_N + 1], minGroups[MAX_N + 1], maxGroups[MAX_N + 1];
long long sumA[MAX_N + 1], sumLowerA[MAX_N + 1], minSwaps[MAX_N + 1];
void computeDP( int n, long long cost ) {
for ( int i = 1; i <= n; i++ ) {
minSwaps[i] = INF;
minGroups[i] = n + 1;
maxGroups[i] = 0;
for ( int j = i - 1; j >= 0; j-- ) {
long long s = minSwaps[j] + i * (countLowerA[i] - j) - (sumLowerA[i] - sumA[j]) + cost;
if ( s < minSwaps[i] ) {
minSwaps[i] = s;
minGroups[i] = minGroups[j] + 1;
maxGroups[i] = maxGroups[j] + 1;
} else if ( s == minSwaps[i] ) {
minGroups[i] = min( minGroups[i], minGroups[j] + 1 );
maxGroups[i] = max( maxGroups[i], maxGroups[j] + 1 );
}
}
}
}
int main() {
int n, k, swaps;
cin >> n >> k;
int countB = 0, countA = 0;
swaps = 0;
for ( int i = 0; i < 2 * n; i++ ) {
char ch;
cin >> ch;
if ( ch == 'A' )
countA++;
else {
countB++;
swaps += max( 0, countB - countA );
a[countB] = max( countA, countB );
sumA[countB] = sumA[countB - 1] + a[countB];
countLowerA[a[countB]]++;
sumLowerA[a[countB]] += a[countB];
}
}
for ( int i = 1; i <= n; i++ ) {
countLowerA[i] += countLowerA[i - 1];
sumLowerA[i] += sumLowerA[i - 1];
}
long long leftCost = -1, rightCost = 1e12;
while ( rightCost - leftCost > 1 ) {
long long cost = (leftCost + rightCost) / 2;
computeDP( n, cost );
if ( minGroups[n] > k )
leftCost = cost;
else
rightCost = cost;
}
computeDP( n, rightCost );
cout << swaps + minSwaps[n] - min( k, maxGroups[n] ) * rightCost;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10584 KB |
Output is correct |
18 |
Correct |
11 ms |
10584 KB |
Output is correct |
19 |
Correct |
9 ms |
10588 KB |
Output is correct |
20 |
Correct |
10 ms |
10720 KB |
Output is correct |
21 |
Correct |
11 ms |
10588 KB |
Output is correct |
22 |
Correct |
12 ms |
10588 KB |
Output is correct |
23 |
Correct |
10 ms |
10720 KB |
Output is correct |
24 |
Correct |
10 ms |
10588 KB |
Output is correct |
25 |
Correct |
10 ms |
10588 KB |
Output is correct |
26 |
Correct |
10 ms |
10588 KB |
Output is correct |
27 |
Correct |
10 ms |
10588 KB |
Output is correct |
28 |
Correct |
11 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10584 KB |
Output is correct |
18 |
Correct |
11 ms |
10584 KB |
Output is correct |
19 |
Correct |
9 ms |
10588 KB |
Output is correct |
20 |
Correct |
10 ms |
10720 KB |
Output is correct |
21 |
Correct |
11 ms |
10588 KB |
Output is correct |
22 |
Correct |
12 ms |
10588 KB |
Output is correct |
23 |
Correct |
10 ms |
10720 KB |
Output is correct |
24 |
Correct |
10 ms |
10588 KB |
Output is correct |
25 |
Correct |
10 ms |
10588 KB |
Output is correct |
26 |
Correct |
10 ms |
10588 KB |
Output is correct |
27 |
Correct |
10 ms |
10588 KB |
Output is correct |
28 |
Correct |
11 ms |
10588 KB |
Output is correct |
29 |
Correct |
685 ms |
10756 KB |
Output is correct |
30 |
Correct |
730 ms |
10784 KB |
Output is correct |
31 |
Correct |
712 ms |
10836 KB |
Output is correct |
32 |
Correct |
738 ms |
10780 KB |
Output is correct |
33 |
Correct |
746 ms |
10776 KB |
Output is correct |
34 |
Correct |
729 ms |
10780 KB |
Output is correct |
35 |
Correct |
804 ms |
10780 KB |
Output is correct |
36 |
Correct |
716 ms |
10780 KB |
Output is correct |
37 |
Correct |
713 ms |
10776 KB |
Output is correct |
38 |
Correct |
716 ms |
10780 KB |
Output is correct |
39 |
Correct |
712 ms |
10780 KB |
Output is correct |
40 |
Correct |
711 ms |
10784 KB |
Output is correct |
41 |
Correct |
698 ms |
10776 KB |
Output is correct |
42 |
Correct |
781 ms |
10776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10584 KB |
Output is correct |
18 |
Correct |
11 ms |
10584 KB |
Output is correct |
19 |
Correct |
9 ms |
10588 KB |
Output is correct |
20 |
Correct |
10 ms |
10720 KB |
Output is correct |
21 |
Correct |
11 ms |
10588 KB |
Output is correct |
22 |
Correct |
12 ms |
10588 KB |
Output is correct |
23 |
Correct |
10 ms |
10720 KB |
Output is correct |
24 |
Correct |
10 ms |
10588 KB |
Output is correct |
25 |
Correct |
10 ms |
10588 KB |
Output is correct |
26 |
Correct |
10 ms |
10588 KB |
Output is correct |
27 |
Correct |
10 ms |
10588 KB |
Output is correct |
28 |
Correct |
11 ms |
10588 KB |
Output is correct |
29 |
Correct |
685 ms |
10756 KB |
Output is correct |
30 |
Correct |
730 ms |
10784 KB |
Output is correct |
31 |
Correct |
712 ms |
10836 KB |
Output is correct |
32 |
Correct |
738 ms |
10780 KB |
Output is correct |
33 |
Correct |
746 ms |
10776 KB |
Output is correct |
34 |
Correct |
729 ms |
10780 KB |
Output is correct |
35 |
Correct |
804 ms |
10780 KB |
Output is correct |
36 |
Correct |
716 ms |
10780 KB |
Output is correct |
37 |
Correct |
713 ms |
10776 KB |
Output is correct |
38 |
Correct |
716 ms |
10780 KB |
Output is correct |
39 |
Correct |
712 ms |
10780 KB |
Output is correct |
40 |
Correct |
711 ms |
10784 KB |
Output is correct |
41 |
Correct |
698 ms |
10776 KB |
Output is correct |
42 |
Correct |
781 ms |
10776 KB |
Output is correct |
43 |
Execution timed out |
7026 ms |
13492 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
3 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
10584 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
10584 KB |
Output is correct |
18 |
Correct |
11 ms |
10584 KB |
Output is correct |
19 |
Correct |
9 ms |
10588 KB |
Output is correct |
20 |
Correct |
10 ms |
10720 KB |
Output is correct |
21 |
Correct |
11 ms |
10588 KB |
Output is correct |
22 |
Correct |
12 ms |
10588 KB |
Output is correct |
23 |
Correct |
10 ms |
10720 KB |
Output is correct |
24 |
Correct |
10 ms |
10588 KB |
Output is correct |
25 |
Correct |
10 ms |
10588 KB |
Output is correct |
26 |
Correct |
10 ms |
10588 KB |
Output is correct |
27 |
Correct |
10 ms |
10588 KB |
Output is correct |
28 |
Correct |
11 ms |
10588 KB |
Output is correct |
29 |
Correct |
685 ms |
10756 KB |
Output is correct |
30 |
Correct |
730 ms |
10784 KB |
Output is correct |
31 |
Correct |
712 ms |
10836 KB |
Output is correct |
32 |
Correct |
738 ms |
10780 KB |
Output is correct |
33 |
Correct |
746 ms |
10776 KB |
Output is correct |
34 |
Correct |
729 ms |
10780 KB |
Output is correct |
35 |
Correct |
804 ms |
10780 KB |
Output is correct |
36 |
Correct |
716 ms |
10780 KB |
Output is correct |
37 |
Correct |
713 ms |
10776 KB |
Output is correct |
38 |
Correct |
716 ms |
10780 KB |
Output is correct |
39 |
Correct |
712 ms |
10780 KB |
Output is correct |
40 |
Correct |
711 ms |
10784 KB |
Output is correct |
41 |
Correct |
698 ms |
10776 KB |
Output is correct |
42 |
Correct |
781 ms |
10776 KB |
Output is correct |
43 |
Execution timed out |
7026 ms |
13492 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |