Submission #1224759

#TimeUsernameProblemLanguageResultExecution timeMemory
1224759_rain_Arranging Tickets (JOI17_arranging_tickets)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 200200;

int n, m, pos = 0;
long long a[N], s[N];
vector<pair<int, int>> upd[N];

bool check (long long take, long long lim){
    fill(s, s + n + 1, 0ll);
    priority_queue<pair<int, int>> pq;
    long long cur = 0;
    for (int i = 1; i <= pos; ++i){
        long long need = max(0,a[i]-lim);
        assert(a[i]-need<=lim);
        for (auto [x, y] : upd[i]){
            if (x > pos){
                pq.push({x, y});
            }
        } 
        while (cur < need){
            if (pq.empty()){
                return false;
            }
            auto [x, y] = pq.top(); pq.pop();
            if (cur + 1ll * y <= need){
                cur += 1ll * y;
                s[x] += 1ll * y;
            } else {
                long long add = need - cur;
                cur += add;
                s[x] += add;
                y -= (int)(add);
                pq.push({x, y});
            }
        }
    }
    for (int i = pos + 1; i <= n; ++i){
        cur -= s[i];
        if (1ll * a[i] + take - 2ll * cur > lim){
            return false;
        }
    }
    return true;
}

int32_t main (){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    if (fopen ("main.inp", "r")){
        freopen ("main.inp", "r", stdin);
        freopen ("main.out", "w", stdout);
    }
    cin >> n >> m;
    for (int i = 1; i <= m; ++i){
        int x, y, z; cin >> x >> y >> z;
        if (x > y){
            swap(x, y);
        }
        a[x] += 1ll * z;
        a[y] -= 1ll * z;
        upd[x].push_back({y, z});
    }
    for (int i = 1; i <= n; ++i){
        a[i] += a[i - 1];
        if (a[pos] < a[i]){
            pos = i;
        }
    }
    long long l = 0, r = 1ll * m * (int)(1e9), ans = r;
    while (l <= r){
        long long mid = l + r >> 1;
        bool possible = (check(a[pos] - mid, mid) || check(a[pos] - mid + 1, mid));
        if (possible){
            ans = mid;
            r = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    cout << ans << "\n";
}

Compilation message (stderr)

arranging_tickets.cpp: In function 'bool check(long long int, long long int)':
arranging_tickets.cpp:15:29: error: no matching function for call to 'max(int, long long int)'
   15 |         long long need = max(0,a[i]-lim);
      |                          ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/11/bits/specfun.h:45,
                 from /usr/include/c++/11/cmath:1935,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:41,
                 from arranging_tickets.cpp:1:
/usr/include/c++/11/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++/11/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
arranging_tickets.cpp:15:29: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   15 |         long long need = max(0,a[i]-lim);
      |                          ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/11/bits/specfun.h:45,
                 from /usr/include/c++/11/cmath:1935,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:41,
                 from arranging_tickets.cpp:1:
/usr/include/c++/11/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++/11/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
arranging_tickets.cpp:15:29: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   15 |         long long need = max(0,a[i]-lim);
      |                          ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/11/string:52,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/istream:38,
                 from /usr/include/c++/11/sstream:38,
                 from /usr/include/c++/11/complex:45,
                 from /usr/include/c++/11/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54,
                 from arranging_tickets.cpp:1:
/usr/include/c++/11/bits/stl_algo.h:3461:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3461 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/11/bits/stl_algo.h:3461:5: note:   template argument deduction/substitution failed:
arranging_tickets.cpp:15:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   15 |         long long need = max(0,a[i]-lim);
      |                          ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/11/string:52,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/istream:38,
                 from /usr/include/c++/11/sstream:38,
                 from /usr/include/c++/11/complex:45,
                 from /usr/include/c++/11/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54,
                 from arranging_tickets.cpp:1:
/usr/include/c++/11/bits/stl_algo.h:3467:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3467 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/11/bits/stl_algo.h:3467:5: note:   template argument deduction/substitution failed:
arranging_tickets.cpp:15:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   15 |         long long need = max(0,a[i]-lim);
      |                          ~~~^~~~~~~~~~~~
arranging_tickets.cpp: In function 'int32_t main()':
arranging_tickets.cpp:51:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen ("main.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
arranging_tickets.cpp:52:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen ("main.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~