Alex_tz307

USACO 2020 US Open Contest, Silver Problem 1. Social Distancing

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