Alex_tz307

USACO 2018 December Contest, Silver Problem 2. Convention II

Dec 2nd, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. // http://www.usaco.org/index.php?page=viewproblem2&cpid=859
  2. #include <bits/stdc++.h>
  3. #define mp make_pair
  4. #define x first
  5. #define y second
  6.  
  7. using namespace std;
  8. using ll = long long;
  9. using pll = pair<ll,ll>;
  10.  
  11. ifstream fin("convention2.in");
  12. ofstream fout("convention2.out");
  13.  
  14. void max_self(ll &a, ll b) {
  15.     a = max(a, b);
  16. }
  17.  
  18. int main() {
  19.     int N;
  20.     fin >> N;
  21.     vector<pair<int,pll>> cows;
  22.     for(int i = 0; i < N; ++i) {
  23.         ll a, t;
  24.         fin >> a >> t;
  25.         cows.push_back(mp(a, mp(i, t)));
  26.     }
  27.     sort(cows.begin(), cows.end());
  28.     ll ans = 0, curr_done = cows[0].x + cows[0].y.y;
  29.     int next = 1;
  30.     set<pll> waiting;
  31.     while(next < N || !waiting.empty()) {
  32.         while(next < N && cows[next].x <= curr_done) {
  33.             waiting.insert(mp(cows[next].y.x, next));
  34.             ++next;
  35.         }
  36.         if(waiting.empty() && next < N) {
  37.             curr_done = cows[next].x + cows[next].y.y;
  38.             ++next;
  39.         }
  40.         else
  41.             if(!waiting.empty()) {
  42.                 set<pll>::iterator next_cow = waiting.begin();
  43.                 max_self(ans, curr_done - cows[next_cow->second].x);
  44.                 curr_done += cows[next_cow->second].y.y;
  45.                 waiting.erase(next_cow);
  46.             }
  47.     }
  48.     fout << ans;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment