#include<bits/stdc++.h>
using namespace std;
struct aaa{
int x,y;
}a[1000005];
bool cmp(aaa x,aaa y){
return x.x<y.x;
}
int n,k;
int check(int x){
// if(x>n) return 1;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
priority_queue<pair<int,int> > p;
int i=0;
for(i=0;a[i].x<x&&i<n;i++){
q.push({a[i].y,a[i].x});
p.push({a[i].y,a[i].x});
}
if(!q.empty()){
if(abs(q.top().first-p.top().first)>=k){
return 1;
}
}
for(;i<n;i++){
q.push({a[i].y,a[i].x});
while(q.top().second<a[i].x-x){
// cout<<q.top().second<<endl;
q.pop();
}
p.push({a[i].y,a[i].x});
while(p.top().second<a[i].x-x){
// cout<<p.top().second<<endl;
p.pop();
}
// cout<<q.top().first<<endl;
if(abs(q.top().first-p.top().first)>=k){
return 1;
}
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i].x>>a[i].y;
}
sort(a,a+n,cmp);
int l=0,r=1e9+10;
while(l<r-1){
// cout<<l<<" "<<r<<endl;
int mid=(l+r)/2;
if(check(mid)==1){
r=mid;
}else{
l=mid;
}
}
if(r==1e9+10) cout<<-1;
else cout<<r<<endl;
return 0;
}写代码
这是 2025年10月31日 存档的第 1 版,不是当前版本。查看当前版本