Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- #include<algorithm>
- using namespace std;
- struct pos{
- int x;
- int y;
- };
- struct elemMax{
- int x;
- int y;
- bool operator < (const elemMax& rhs)const
- {
- return y < rhs.y;
- }
- };
- struct elemMin{
- int x;
- int y;
- bool operator < (const elemMin& rhs)const
- {
- return y > rhs.y;
- }
- };
- bool Xcmp(pos a, pos b){
- return a.x < b.x;
- }
- bool YMax(pos a,pos b){
- return a.y > b.y;
- }
- int main()
- {
- int Q;
- scanf("%d",&Q);
- for(int t = 0 ; t < Q ; t ++)
- {
- int n,h;
- scanf("%d %d",&n,&h);
- pos arr[n];
- int minX = 1e9;
- int maxX = -1e9;
- for(int i = 0 ; i < n ; i ++){
- scanf("%d %d",&arr[i].x,&arr[i].y);
- minX = min(minX,arr[i].x);
- maxX = max(maxX,arr[i].x);
- }
- int minlen = maxX - minX;
- bool check = false;
- sort(arr,arr+n,Xcmp);
- priority_queue<elemMax> pqMax;
- priority_queue<elemMin> pqMin;
- pqMax.push({arr[0].x,arr[0].y});
- pqMin.push({arr[0].x,arr[0].y});
- for(int i = 1 ; i < n ; i ++){
- int X = arr[i].x;
- int Y = arr[i].y;
- while(!pqMax.empty() && X - pqMax.top().x > minlen){
- pqMax.pop();
- }
- while(!pqMin.empty() && X - pqMin.top().x > minlen){
- pqMin.pop();
- }
- if(!pqMax.empty() && pqMax.top().y - Y >= h){
- minlen = min(minlen , X - pqMax.top().x);
- check = true;
- }
- if(!pqMax.empty() && Y - pqMin.top().y >= h){
- minlen = min(minlen , X - pqMin.top().x);
- check = true;
- }
- pqMax.push({X,Y});
- pqMin.push({X,Y});
- }
- printf("%d\n",(check ? minlen : -1));
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment