SuitNdtie

XmasBeam

May 30th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. struct pos{
  7.     int x;
  8.     int y;
  9.  
  10. };
  11.  
  12. struct elemMax{
  13.     int x;
  14.     int y;
  15.     bool operator < (const elemMax& rhs)const
  16.     {
  17.         return y < rhs.y;
  18.     }
  19. };
  20.  
  21. struct elemMin{
  22.     int x;
  23.     int y;
  24.     bool operator < (const elemMin& rhs)const
  25.     {
  26.         return y > rhs.y;
  27.     }
  28. };
  29.  
  30. bool Xcmp(pos a, pos b){
  31.     return a.x < b.x;
  32. }
  33. bool YMax(pos a,pos b){
  34.     return a.y > b.y;
  35. }
  36. int main()
  37. {
  38.     int Q;
  39.     scanf("%d",&Q);
  40.     for(int t = 0 ; t < Q ; t ++)
  41.     {
  42.         int n,h;
  43.         scanf("%d %d",&n,&h);
  44.         pos arr[n];
  45.         int minX = 1e9;
  46.         int maxX = -1e9;
  47.         for(int i = 0 ; i < n ; i ++){
  48.             scanf("%d %d",&arr[i].x,&arr[i].y);
  49.             minX = min(minX,arr[i].x);
  50.             maxX = max(maxX,arr[i].x);
  51.         }
  52.         int minlen = maxX - minX;
  53.         bool check = false;
  54.         sort(arr,arr+n,Xcmp);
  55.         priority_queue<elemMax> pqMax;
  56.         priority_queue<elemMin> pqMin;
  57.  
  58.         pqMax.push({arr[0].x,arr[0].y});
  59.         pqMin.push({arr[0].x,arr[0].y});
  60.  
  61.         for(int i = 1 ; i < n ; i ++){
  62.             int X = arr[i].x;
  63.             int Y = arr[i].y;
  64.  
  65.             while(!pqMax.empty() && X - pqMax.top().x > minlen){
  66.                 pqMax.pop();
  67.             }
  68.             while(!pqMin.empty() && X - pqMin.top().x > minlen){
  69.                 pqMin.pop();
  70.             }
  71.  
  72.  
  73.             if(!pqMax.empty() && pqMax.top().y - Y >= h){
  74.                 minlen = min(minlen , X - pqMax.top().x);
  75.                 check = true;
  76.             }
  77.             if(!pqMax.empty() && Y - pqMin.top().y >= h){
  78.                 minlen = min(minlen , X - pqMin.top().x);
  79.                 check = true;
  80.             }
  81.  
  82.             pqMax.push({X,Y});
  83.             pqMin.push({X,Y});
  84.  
  85.         }
  86.         printf("%d\n",(check ? minlen : -1));
  87.     }
  88.     return 0;
  89. }
Add Comment
Please, Sign In to add comment