Submission #906576

# Submission time Handle Problem Language Result Execution time Memory
906576 2024-01-14T13:27:04 Z LucaIlie Chorus (JOI23_chorus) C++17
100 / 100
5789 ms 137328 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct DP {
    long long minSwaps;
    int minGroups, maxGroups;

    DP operator + ( DP x ) {
        return { minSwaps + x.minSwaps, minGroups + x.minGroups, maxGroups + x.maxGroups };
    }

    DP operator * ( DP x ) {
        if ( minSwaps < x.minSwaps )
            return *this;
        if ( minSwaps > x.minSwaps )
            return x;
        return { minSwaps, min( minGroups, x.minGroups ), max( maxGroups, x.maxGroups ) };
    }
};

const int MAX_N = 1e6;
const long long INF = 1e18;
int a[MAX_N + 1], countLowerA[MAX_N + 1];
long long sumA[MAX_N + 1], sumLowerA[MAX_N + 1];
DP dp[MAX_N + 1];

struct func {
    long long a, b;
    int valmin, valmax;

    long long value( long long x ) {
        return a * x + b;
    }
};

struct CHT {
    vector<func> funcs;
    vector<long long> start;
    vector<int> stack;
    DP dppp[MAX_N + 1];

    void init() {
        for ( int i = 0; i <= MAX_N; i++ )
            dppp[i] = { INF, MAX_N + 1, 0 };
        funcs.clear();
        start.clear();
        stack.clear();
    }

    long double intersection( func f1, func f2 ) {
        return ((long double)f2.b - f1.b) / (f1.a - f2.a);
    }

    void addFunction( func f ) {
        int i = funcs.size();

        funcs.push_back( f );
        start.push_back( 0 );
        while ( !stack.empty() && intersection( funcs[i], funcs[stack.back()] ) <= start[stack.back()] )
            stack.pop_back();

        if ( !stack.empty() ) {
            long long x = intersection( funcs[i], funcs[stack.back()] );
            if ( intersection( funcs[i], funcs[stack.back()] ) == x && x <= MAX_N )
                dppp[x] = dppp[x] * DP{ funcs[stack.back()].value( x ), funcs[stack.back()].valmin, funcs[stack.back()].valmax };
            start[i] = ceil( intersection( funcs[i], funcs[stack.back()]) );
        }
        stack.push_back( i );
    }

    DP getMin( long long x ) {
        int l = -1, r = stack.size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;
            if ( start[stack[mid]] > x )
                r = mid;
            else
                l = mid;
        }
        return DP{ funcs[stack[l]].value( x ), funcs[stack[l]].valmin, funcs[stack[l]].valmax } * dppp[x];
    }
} swapFunctions;

void computeDP( int n, long long cost ) {
    swapFunctions.init();
    swapFunctions.addFunction( { 0, 0, 0, 0 } );
    for ( int i = 1; i <= n; i++ ) {
        dp[i] = swapFunctions.getMin( i ) + DP{ i * countLowerA[i] - sumLowerA[i] + cost, 1, 1 };
        swapFunctions.addFunction( func{ -i, dp[i].minSwaps + sumA[i], dp[i].minGroups, dp[i].maxGroups } );
    }
}

signed 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( 0LL, 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 ( dp[n].minGroups > k )
            leftCost = cost;
        else
            rightCost = cost;
    }

    computeDP( n, rightCost );
    cout << swaps + dp[n].minSwaps - min( k, dp[n].maxGroups ) * rightCost;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 121 ms 31324 KB Output is correct
2 Correct 121 ms 31388 KB Output is correct
3 Correct 122 ms 31324 KB Output is correct
4 Correct 124 ms 31396 KB Output is correct
5 Correct 123 ms 31384 KB Output is correct
6 Correct 127 ms 31380 KB Output is correct
7 Correct 122 ms 31320 KB Output is correct
8 Correct 125 ms 31396 KB Output is correct
9 Correct 123 ms 31380 KB Output is correct
10 Correct 126 ms 31324 KB Output is correct
11 Correct 123 ms 31384 KB Output is correct
12 Correct 121 ms 31328 KB Output is correct
13 Correct 120 ms 31324 KB Output is correct
14 Correct 123 ms 31320 KB Output is correct
15 Correct 124 ms 31572 KB Output is correct
16 Correct 129 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 31324 KB Output is correct
2 Correct 121 ms 31388 KB Output is correct
3 Correct 122 ms 31324 KB Output is correct
4 Correct 124 ms 31396 KB Output is correct
5 Correct 123 ms 31384 KB Output is correct
6 Correct 127 ms 31380 KB Output is correct
7 Correct 122 ms 31320 KB Output is correct
8 Correct 125 ms 31396 KB Output is correct
9 Correct 123 ms 31380 KB Output is correct
10 Correct 126 ms 31324 KB Output is correct
11 Correct 123 ms 31384 KB Output is correct
12 Correct 121 ms 31328 KB Output is correct
13 Correct 120 ms 31324 KB Output is correct
14 Correct 123 ms 31320 KB Output is correct
15 Correct 124 ms 31572 KB Output is correct
16 Correct 129 ms 31324 KB Output is correct
17 Correct 120 ms 31400 KB Output is correct
18 Correct 129 ms 31324 KB Output is correct
19 Correct 121 ms 31572 KB Output is correct
20 Correct 129 ms 31324 KB Output is correct
21 Correct 123 ms 31324 KB Output is correct
22 Correct 127 ms 31420 KB Output is correct
23 Correct 120 ms 31320 KB Output is correct
24 Correct 126 ms 31324 KB Output is correct
25 Correct 120 ms 31424 KB Output is correct
26 Correct 125 ms 31568 KB Output is correct
27 Correct 122 ms 31324 KB Output is correct
28 Correct 125 ms 31416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 31324 KB Output is correct
2 Correct 121 ms 31388 KB Output is correct
3 Correct 122 ms 31324 KB Output is correct
4 Correct 124 ms 31396 KB Output is correct
5 Correct 123 ms 31384 KB Output is correct
6 Correct 127 ms 31380 KB Output is correct
7 Correct 122 ms 31320 KB Output is correct
8 Correct 125 ms 31396 KB Output is correct
9 Correct 123 ms 31380 KB Output is correct
10 Correct 126 ms 31324 KB Output is correct
11 Correct 123 ms 31384 KB Output is correct
12 Correct 121 ms 31328 KB Output is correct
13 Correct 120 ms 31324 KB Output is correct
14 Correct 123 ms 31320 KB Output is correct
15 Correct 124 ms 31572 KB Output is correct
16 Correct 129 ms 31324 KB Output is correct
17 Correct 120 ms 31400 KB Output is correct
18 Correct 129 ms 31324 KB Output is correct
19 Correct 121 ms 31572 KB Output is correct
20 Correct 129 ms 31324 KB Output is correct
21 Correct 123 ms 31324 KB Output is correct
22 Correct 127 ms 31420 KB Output is correct
23 Correct 120 ms 31320 KB Output is correct
24 Correct 126 ms 31324 KB Output is correct
25 Correct 120 ms 31424 KB Output is correct
26 Correct 125 ms 31568 KB Output is correct
27 Correct 122 ms 31324 KB Output is correct
28 Correct 125 ms 31416 KB Output is correct
29 Correct 137 ms 31832 KB Output is correct
30 Correct 144 ms 31836 KB Output is correct
31 Correct 137 ms 31836 KB Output is correct
32 Correct 132 ms 31836 KB Output is correct
33 Correct 143 ms 31832 KB Output is correct
34 Correct 139 ms 31832 KB Output is correct
35 Correct 135 ms 31832 KB Output is correct
36 Correct 141 ms 31836 KB Output is correct
37 Correct 143 ms 31836 KB Output is correct
38 Correct 140 ms 31836 KB Output is correct
39 Correct 143 ms 31832 KB Output is correct
40 Correct 138 ms 31832 KB Output is correct
41 Correct 139 ms 31832 KB Output is correct
42 Correct 138 ms 31680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 31324 KB Output is correct
2 Correct 121 ms 31388 KB Output is correct
3 Correct 122 ms 31324 KB Output is correct
4 Correct 124 ms 31396 KB Output is correct
5 Correct 123 ms 31384 KB Output is correct
6 Correct 127 ms 31380 KB Output is correct
7 Correct 122 ms 31320 KB Output is correct
8 Correct 125 ms 31396 KB Output is correct
9 Correct 123 ms 31380 KB Output is correct
10 Correct 126 ms 31324 KB Output is correct
11 Correct 123 ms 31384 KB Output is correct
12 Correct 121 ms 31328 KB Output is correct
13 Correct 120 ms 31324 KB Output is correct
14 Correct 123 ms 31320 KB Output is correct
15 Correct 124 ms 31572 KB Output is correct
16 Correct 129 ms 31324 KB Output is correct
17 Correct 120 ms 31400 KB Output is correct
18 Correct 129 ms 31324 KB Output is correct
19 Correct 121 ms 31572 KB Output is correct
20 Correct 129 ms 31324 KB Output is correct
21 Correct 123 ms 31324 KB Output is correct
22 Correct 127 ms 31420 KB Output is correct
23 Correct 120 ms 31320 KB Output is correct
24 Correct 126 ms 31324 KB Output is correct
25 Correct 120 ms 31424 KB Output is correct
26 Correct 125 ms 31568 KB Output is correct
27 Correct 122 ms 31324 KB Output is correct
28 Correct 125 ms 31416 KB Output is correct
29 Correct 137 ms 31832 KB Output is correct
30 Correct 144 ms 31836 KB Output is correct
31 Correct 137 ms 31836 KB Output is correct
32 Correct 132 ms 31836 KB Output is correct
33 Correct 143 ms 31832 KB Output is correct
34 Correct 139 ms 31832 KB Output is correct
35 Correct 135 ms 31832 KB Output is correct
36 Correct 141 ms 31836 KB Output is correct
37 Correct 143 ms 31836 KB Output is correct
38 Correct 140 ms 31836 KB Output is correct
39 Correct 143 ms 31832 KB Output is correct
40 Correct 138 ms 31832 KB Output is correct
41 Correct 139 ms 31832 KB Output is correct
42 Correct 138 ms 31680 KB Output is correct
43 Correct 351 ms 38604 KB Output is correct
44 Correct 535 ms 42744 KB Output is correct
45 Correct 524 ms 41676 KB Output is correct
46 Correct 366 ms 41924 KB Output is correct
47 Correct 418 ms 41872 KB Output is correct
48 Correct 499 ms 42692 KB Output is correct
49 Correct 469 ms 42956 KB Output is correct
50 Correct 519 ms 42692 KB Output is correct
51 Correct 533 ms 42956 KB Output is correct
52 Correct 480 ms 43212 KB Output is correct
53 Correct 490 ms 42272 KB Output is correct
54 Correct 540 ms 41668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 31324 KB Output is correct
2 Correct 121 ms 31388 KB Output is correct
3 Correct 122 ms 31324 KB Output is correct
4 Correct 124 ms 31396 KB Output is correct
5 Correct 123 ms 31384 KB Output is correct
6 Correct 127 ms 31380 KB Output is correct
7 Correct 122 ms 31320 KB Output is correct
8 Correct 125 ms 31396 KB Output is correct
9 Correct 123 ms 31380 KB Output is correct
10 Correct 126 ms 31324 KB Output is correct
11 Correct 123 ms 31384 KB Output is correct
12 Correct 121 ms 31328 KB Output is correct
13 Correct 120 ms 31324 KB Output is correct
14 Correct 123 ms 31320 KB Output is correct
15 Correct 124 ms 31572 KB Output is correct
16 Correct 129 ms 31324 KB Output is correct
17 Correct 120 ms 31400 KB Output is correct
18 Correct 129 ms 31324 KB Output is correct
19 Correct 121 ms 31572 KB Output is correct
20 Correct 129 ms 31324 KB Output is correct
21 Correct 123 ms 31324 KB Output is correct
22 Correct 127 ms 31420 KB Output is correct
23 Correct 120 ms 31320 KB Output is correct
24 Correct 126 ms 31324 KB Output is correct
25 Correct 120 ms 31424 KB Output is correct
26 Correct 125 ms 31568 KB Output is correct
27 Correct 122 ms 31324 KB Output is correct
28 Correct 125 ms 31416 KB Output is correct
29 Correct 137 ms 31832 KB Output is correct
30 Correct 144 ms 31836 KB Output is correct
31 Correct 137 ms 31836 KB Output is correct
32 Correct 132 ms 31836 KB Output is correct
33 Correct 143 ms 31832 KB Output is correct
34 Correct 139 ms 31832 KB Output is correct
35 Correct 135 ms 31832 KB Output is correct
36 Correct 141 ms 31836 KB Output is correct
37 Correct 143 ms 31836 KB Output is correct
38 Correct 140 ms 31836 KB Output is correct
39 Correct 143 ms 31832 KB Output is correct
40 Correct 138 ms 31832 KB Output is correct
41 Correct 139 ms 31832 KB Output is correct
42 Correct 138 ms 31680 KB Output is correct
43 Correct 351 ms 38604 KB Output is correct
44 Correct 535 ms 42744 KB Output is correct
45 Correct 524 ms 41676 KB Output is correct
46 Correct 366 ms 41924 KB Output is correct
47 Correct 418 ms 41872 KB Output is correct
48 Correct 499 ms 42692 KB Output is correct
49 Correct 469 ms 42956 KB Output is correct
50 Correct 519 ms 42692 KB Output is correct
51 Correct 533 ms 42956 KB Output is correct
52 Correct 480 ms 43212 KB Output is correct
53 Correct 490 ms 42272 KB Output is correct
54 Correct 540 ms 41668 KB Output is correct
55 Correct 3434 ms 107808 KB Output is correct
56 Correct 5283 ms 137328 KB Output is correct
57 Correct 5468 ms 127940 KB Output is correct
58 Correct 2873 ms 128444 KB Output is correct
59 Correct 3801 ms 134008 KB Output is correct
60 Correct 5088 ms 135780 KB Output is correct
61 Correct 4944 ms 135984 KB Output is correct
62 Correct 5315 ms 127824 KB Output is correct
63 Correct 5459 ms 128660 KB Output is correct
64 Correct 5054 ms 136624 KB Output is correct
65 Correct 5016 ms 135772 KB Output is correct
66 Correct 5789 ms 127908 KB Output is correct
67 Correct 5047 ms 136880 KB Output is correct