Alex_tz307

USACO 2017 US Open Contest, Silver Problem 1. Paired Up

Dec 4th, 2020
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. // http://www.usaco.org/index.php?page=viewproblem2&cpid=738
  2. #include <bits/stdc++.h>
  3. #define pi pair<int,int>
  4. #define x first
  5. #define y second
  6.  
  7. using namespace std;
  8.  
  9. ifstream fin("pairup.in");
  10. ofstream fout("pairup.out");
  11.  
  12. bool fcmp(const pi &a, const pi &b) {
  13.     return a.y < b.y;
  14. }
  15.  
  16. void max_self(int &a, int b) {
  17.     a = max(a, b);
  18. }
  19.  
  20. int main() {
  21.     int N;
  22.     fin >> N;
  23.     vector<pi> a(N);
  24.     for(int i = 0; i < N; ++i)
  25.         fin >> a[i].x >> a[i].y;
  26.     sort(a.begin(), a.end(), fcmp);
  27.     int p = N - 1, ans = 0;
  28.     for(int i = 0; i <= p; ++i) {
  29.         max_self(ans, a[i].y + a[p].y);
  30.         if(a[i].x > a[p].x) {
  31.             a[i].x -= a[p].x;
  32.             --p;
  33.             --i;
  34.         }
  35.         else {
  36.             a[p].x -= a[i].x;
  37.             if(a[p].x == 0)
  38.                 --p;
  39.         }
  40.     }
  41.     fout << ans;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment