Submission #906575

# Submission time Handle Problem Language Result Execution time Memory
906575 2024-01-14T13:26:46 Z LucaIlie Chorus (JOI23_chorus) C++17
Compilation error
0 ms 0 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( 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 ( 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 function 'int main()':
chorus.cpp:108:46: error: no matching function for call to 'max(int, long long int)'
  108 |             swaps += max( 0, countB - countA );
      |                                              ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from chorus.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
chorus.cpp:108:46: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  108 |             swaps += max( 0, countB - countA );
      |                                              ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from chorus.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
chorus.cpp:108:46: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  108 |             swaps += max( 0, countB - countA );
      |                                              ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from chorus.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3480 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note:   template argument deduction/substitution failed:
chorus.cpp:108:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  108 |             swaps += max( 0, countB - countA );
      |                                              ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from chorus.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3486 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note:   template argument deduction/substitution failed:
chorus.cpp:108:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  108 |             swaps += max( 0, countB - countA );
      |                                              ^