[USACO12MAR]花盆Flowerpot
2019-06-11 13:31 一只弱鸡丶 阅读(272) 评论(0) 编辑 收藏 举报#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define ll long long #define re register #define fi first #define se second const int N=1e5+10; inline void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } pair <int,int> q[N]; int maxq[N],minq[N]; int ans=0x3fffffff; int main() { int n,m; read(n); read(m); int h1,h2,t1,t2; h1=h2=1; t1=t2=1; for(re int i=1;i<=n;i++) { read(q[i].fi); read(q[i].se); } sort(q+1,q+1+n); for(int l=1,r=0;l<=n;l++) { while(h1<t1&&maxq[h1]<l) h1++; while(h2<t2&&minq[h2]<l) h2++; while(r<n&&q[maxq[h1]].se-q[minq[h2]].se<m) { r++; while(h1<t1&&q[maxq[t1-1]].se<=q[r].se) t1--; maxq[t1++]=r; while(h2<t2&&q[minq[t2-1]].se>=q[r].se) t2--; minq[t2++]=r; } if(q[maxq[h1]].se-q[minq[h2]].se>=m) ans=min(ans,q[r].fi-q[l].fi); } printf("%d",ans>=0x3fffffff?-1:ans); return 0; }