답안 #906529

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
906529 2024-01-14T12:00:31 Z LucaIlie Chorus (JOI23_chorus) C++17
61 / 100
7000 ms 12236 KB
#include <bits/stdc++.h>

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;
};

struct CHT {
    vector<func> funcs;
    vector<long double> start;
    vector<int> stack;

    void init() {
        funcs.clear();
        start.clear();
        stack.clear();
    }

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

    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[i] )
            stack.pop_back();

        if ( !stack.empty() )
            start[i] = intersection( funcs[i], funcs[stack.back()] );
        stack.push_back( i );
    }

    DP getMin( long long x ) {
        DP ans = { INF, 0, 0 };
        for ( int i = 0; i < stack.size(); i++ ) {
            if ( start[stack[i]] <= x && (i == stack.size() - 1 || x <= start[stack[i + 1]]) )
                ans = ans * DP{ funcs[stack[i]].a * x + funcs[stack[i]].b, funcs[stack[i]].valmin, funcs[stack[i]].valmax };
        }
        return ans;
    }
} 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 } );
    }
}

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 = 0, 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;
}

Compilation message

chorus.cpp: In member function 'DP CHT::getMin(long long int)':
chorus.cpp:64:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         for ( int i = 0; i < stack.size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~~
chorus.cpp:65:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             if ( start[stack[i]] <= x && (i == stack.size() - 1 || x <= start[stack[i + 1]]) )
      |                                           ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 15 ms 6648 KB Output is correct
19 Correct 15 ms 6492 KB Output is correct
20 Correct 17 ms 6644 KB Output is correct
21 Correct 14 ms 6492 KB Output is correct
22 Correct 15 ms 6492 KB Output is correct
23 Correct 15 ms 6492 KB Output is correct
24 Correct 15 ms 6644 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 15 ms 6648 KB Output is correct
27 Correct 16 ms 6492 KB Output is correct
28 Correct 15 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 15 ms 6648 KB Output is correct
19 Correct 15 ms 6492 KB Output is correct
20 Correct 17 ms 6644 KB Output is correct
21 Correct 14 ms 6492 KB Output is correct
22 Correct 15 ms 6492 KB Output is correct
23 Correct 15 ms 6492 KB Output is correct
24 Correct 15 ms 6644 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 15 ms 6648 KB Output is correct
27 Correct 16 ms 6492 KB Output is correct
28 Correct 15 ms 6492 KB Output is correct
29 Correct 1352 ms 7048 KB Output is correct
30 Correct 1362 ms 7004 KB Output is correct
31 Correct 1368 ms 7052 KB Output is correct
32 Correct 1198 ms 7004 KB Output is correct
33 Correct 1208 ms 7244 KB Output is correct
34 Correct 1386 ms 7252 KB Output is correct
35 Correct 1368 ms 7052 KB Output is correct
36 Correct 1342 ms 7048 KB Output is correct
37 Correct 1407 ms 7052 KB Output is correct
38 Correct 1358 ms 7052 KB Output is correct
39 Correct 1389 ms 7052 KB Output is correct
40 Correct 1373 ms 7004 KB Output is correct
41 Correct 1446 ms 7048 KB Output is correct
42 Correct 1157 ms 7052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 15 ms 6648 KB Output is correct
19 Correct 15 ms 6492 KB Output is correct
20 Correct 17 ms 6644 KB Output is correct
21 Correct 14 ms 6492 KB Output is correct
22 Correct 15 ms 6492 KB Output is correct
23 Correct 15 ms 6492 KB Output is correct
24 Correct 15 ms 6644 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 15 ms 6648 KB Output is correct
27 Correct 16 ms 6492 KB Output is correct
28 Correct 15 ms 6492 KB Output is correct
29 Correct 1352 ms 7048 KB Output is correct
30 Correct 1362 ms 7004 KB Output is correct
31 Correct 1368 ms 7052 KB Output is correct
32 Correct 1198 ms 7004 KB Output is correct
33 Correct 1208 ms 7244 KB Output is correct
34 Correct 1386 ms 7252 KB Output is correct
35 Correct 1368 ms 7052 KB Output is correct
36 Correct 1342 ms 7048 KB Output is correct
37 Correct 1407 ms 7052 KB Output is correct
38 Correct 1358 ms 7052 KB Output is correct
39 Correct 1389 ms 7052 KB Output is correct
40 Correct 1373 ms 7004 KB Output is correct
41 Correct 1446 ms 7048 KB Output is correct
42 Correct 1157 ms 7052 KB Output is correct
43 Execution timed out 7019 ms 12236 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 15 ms 6648 KB Output is correct
19 Correct 15 ms 6492 KB Output is correct
20 Correct 17 ms 6644 KB Output is correct
21 Correct 14 ms 6492 KB Output is correct
22 Correct 15 ms 6492 KB Output is correct
23 Correct 15 ms 6492 KB Output is correct
24 Correct 15 ms 6644 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 15 ms 6648 KB Output is correct
27 Correct 16 ms 6492 KB Output is correct
28 Correct 15 ms 6492 KB Output is correct
29 Correct 1352 ms 7048 KB Output is correct
30 Correct 1362 ms 7004 KB Output is correct
31 Correct 1368 ms 7052 KB Output is correct
32 Correct 1198 ms 7004 KB Output is correct
33 Correct 1208 ms 7244 KB Output is correct
34 Correct 1386 ms 7252 KB Output is correct
35 Correct 1368 ms 7052 KB Output is correct
36 Correct 1342 ms 7048 KB Output is correct
37 Correct 1407 ms 7052 KB Output is correct
38 Correct 1358 ms 7052 KB Output is correct
39 Correct 1389 ms 7052 KB Output is correct
40 Correct 1373 ms 7004 KB Output is correct
41 Correct 1446 ms 7048 KB Output is correct
42 Correct 1157 ms 7052 KB Output is correct
43 Execution timed out 7019 ms 12236 KB Time limit exceeded
44 Halted 0 ms 0 KB -