Submission #41565

#TimeUsernameProblemLanguageResultExecution timeMemory
41565grumpy_gordonArranging Tickets (JOI17_arranging_tickets)C++14
100 / 100
1181 ms39260 KiB
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define SZ(v) ((int)(v).size()) #define ALL(v) (v).begin(),(v).end() #define one first #define two second typedef long long ll; typedef pair<int, int> pi; typedef pair<ll, int> pli; typedef pair<ll, pi> plp; typedef tuple<int, int, int> ti; typedef tuple<ll, int, int> tli; const int INF = 0x3f2f1f0f; const ll LINF = 1ll * INF * INF; const int MAX_N = 2e5 + 100, MAX_M = 1e5 + 100, MAX_C = 1e9 + 100; int N, M; ti Nr[MAX_M]; ll Sum[MAX_N], Temp[MAX_N], maxV; bool isCan(ll use, ll lim) { ll up = lim - use; ll cnt = 0; priority_queue<pi> Q; for(int i=0; i<=N; i++) Temp[i] = 0; for(int i=1, j=0; i<=N; i++) { Temp[i] += Temp[i-1]; while(j < M && get<0>(Nr[j]) == i) Q.push(pi(get<1>(Nr[j]), get<2>(Nr[j]))), j++; ll now = Sum[i] + Temp[i]; while(!Q.empty() && now > up) { int y, c; tie(y, c) = Q.top(); Q.pop(); if(y <= i) continue; int v = min((ll)c, (now - up + 1) / 2); Temp[i] -= 2 * v; Temp[y] += 2 * v; cnt += v; now -= 2 * v; if(c - v > 0) Q.push(pi(y, c-v)); } if(now > up) return false; if(cnt > use) return false; } return true; } int main() { cin >> N >> M; for(int i=0; i<M; i++) { int x, y, c; scanf("%d%d%d", &x, &y, &c); if(x > y) swap(x, y); Nr[i] = ti(x, y, c); Sum[x] += c; Sum[y] -= c; } sort(Nr, Nr+M); for(int i=1; i<=N; i++) Sum[i] += Sum[i-1], maxV = max(maxV, Sum[i]); ll ans = -1; for(ll l=0, r=maxV; l<=r; ) { ll m = (l+r) >> 1; if(isCan(maxV-m, m) || isCan(maxV-m+1, m)) ans = m, r = m-1; else l = m+1; } printf("%lld\n", ans); return 0; }

Compilation message (stderr)

arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:47:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y, c; scanf("%d%d%d", &x, &y, &c);
                                           ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...