Ankit_132

D

Dec 6th, 2023
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n;
  14.         cin>>n;
  15.  
  16.         vector<pair<ll int, ll int>> vp(n);
  17.         for(auto &[l, r]: vp)       cin>>l>>r;
  18.  
  19.         ll int ans=1e9, low=0, high=1e9, mid;
  20.  
  21.         auto poss = [&](int k)
  22.         {
  23.             ll int x=0, y=0;
  24.  
  25.             for(auto [a, b]: vp)
  26.             {
  27.                 x -= k;
  28.                 y += k;
  29.  
  30.                 x = max(x, a);
  31.                 y = min(y, b);
  32.  
  33.                 if(x > y)       return false;
  34.             }
  35.  
  36.             return true;
  37.         };
  38.  
  39.         while(low <= high)
  40.         {
  41.             int mid = (low + high)/2;
  42.  
  43.             if(poss(mid))   ans=mid, high=mid-1;
  44.             else            low=mid+1;
  45.         }
  46.  
  47.         cout<<ans<<"\n";
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment