刷水~
Code:
#include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in","r",stdin) #define maxn 300000 #define inf 1000000 deque<int>p,q; struct Node { int x,y; }nd[maxn]; bool cmp(Node a, Node b) { return a.x < b.x; } int main() { // setIO("input"); int n,D,ans=inf; scanf("%d%d",&n,&D); for(int i=1;i<=n;++i) { scanf("%d%d",&nd[i].x,&nd[i].y); } sort(nd+1,nd+1+n,cmp); for(int i=1;i<=n;++i) { while(!p.empty()&&nd[i].y>=nd[p.back()].y) p.pop_back(); while(!q.empty()&&nd[i].y<=nd[q.back()].y) q.pop_back(); p.push_back(i), q.push_back(i); int pre=0; while(!p.empty()&&nd[p.front()].y-nd[i].y>=D) { pre=nd[p.front()].x; p.pop_front(); } if(pre) ans=min(ans, nd[i].x - pre); pre=0; while(!q.empty()&&nd[i].y-nd[q.front()].y>=D) { pre=nd[q.front()].x; q.pop_front(); } if(pre) ans=min(ans, nd[i].x - pre); } printf("%d ",ans==inf?-1:ans); return 0; }