Advertisement
mickypinata

CUBE-T133: Xmas Beam

Sep 12th, 2021
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int, int> pii;
  5.  
  6. const int N = 1e6;
  7.  
  8. int n, h;
  9. pii coor[N + 1];
  10.  
  11. bool BSCheck(int x){
  12.     deque<pii> swMin, swMax;
  13.     for(int i = 1; i <= n; ++i){
  14.         // SW Insert
  15.         while(!swMin.empty() && swMin.back().first >= coor[i].second){
  16.             swMin.pop_back();
  17.         }
  18.         swMin.emplace_back(coor[i].second, coor[i].first);
  19.         while(!swMax.empty() && swMax.back().first <= coor[i].second){
  20.             swMax.pop_back();
  21.         }
  22.         swMax.emplace_back(coor[i].second, coor[i].first);
  23.         // SW Query
  24.         while(!swMin.empty() && swMin.front().second < coor[i].first - x){
  25.             swMin.pop_front();
  26.         }
  27.         while(!swMax.empty() && swMax.front().second < coor[i].first - x){
  28.             swMax.pop_front();
  29.         }
  30.         if(swMin.empty() || swMax.empty()){
  31.             return false;
  32.         }
  33.         if(swMax.front().first - swMin.front().first >= h){
  34.             return true;
  35.         }
  36.     }
  37.     return false;
  38. }
  39.  
  40. int main(){
  41.  
  42.     int Q;
  43.     scanf("%d", &Q);
  44.     while(Q--){
  45.         scanf("%d%d", &n, &h);
  46.         for(int i = 1; i <= n; ++i){
  47.             scanf("%d%d", &coor[i].first, &coor[i].second);
  48.         }
  49.         sort(coor + 1, coor + n + 1);
  50.         if(!BSCheck(1e6)){
  51.             cout << "-1\n";
  52.             continue;
  53.         }
  54.         int l = 1;
  55.         int r = 1e6;
  56.         int mn = 1e6;
  57.         while(l <= r){
  58.             int m = (l + r) / 2;
  59.             if(BSCheck(m)){
  60.                 mn = min(mn, m);
  61.                 r = m - 1;
  62.             } else {
  63.                 l = m + 1;
  64.             }
  65.         }
  66.         cout << mn << '\n';
  67.     }
  68.  
  69.     return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement