滑动窗口的好题
从大到小算一次即可
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int head=1,tail=1,j=2,minn=0X3f3f3f3f;
int n,d;
struct work
{
int x,y;
}a[100001],f[1000001];
bool cmp(work a,work b)
{
return a.x<b.x;
}
int main()
{
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp);
f[1]=a[1];
while(j<=n)
{
while((f[tail].y>a[j].y)&&(tail>=head)) tail--;
tail++;
f[tail]=a[j];
while((tail>=head)&&(f[tail].y-f[head].y>=d))
{
minn=min(minn,f[tail].x-f[head].x);
head++;
}
j++;
}
if(minn==0X3f3f3f3f) printf("-1");
else printf("%d\n",minn);
return 0;
}