WA on #2#4#5 求调
查看原帖
WA on #2#4#5 求调
1002529
fp0cy1tz6mn4rd_楼主2024/9/17 18:28
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
const int N=1e5+5;
typedef struct Position{
	int x,y;
	Position(){}
	Position(int _x,int _y):x(_x),y(_y){}
	friend bool operator<(Position a,Position b){
		if(a.x==b.x) return a.y<b.y;
		return a.x<b.x;
	}
} pos;
int n,d,x,h;
pos a[N];
bool check(int x){
	deque<int> q1,q2;
	for(int i=1;i<=n;i++){
		while(!q1.empty()&&a[i].x>a[q1.front()].x+x) q1.pop_front();
		while(!q2.empty()&&a[i].x>a[q2.front()].x+x) q2.pop_front();
		while(!q1.empty()&&a[q1.back()].y<a[i].y) q1.pop_back();
		while(!q2.empty()&&a[q2.back()].y>a[i].y) q2.pop_back();
		q1.push_back(i);
		q2.push_back(i);
		if(i>=x&&a[q1.front()].y-a[q2.front()].y>=d) return true;
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>d;
	for(int i=1;i<=n;i++){
		cin>>x>>h;
		a[i]=pos(x,h);
	}
	sort(a+1,a+n+1);
	int l=1,r=n+1;
	while(l<r){
		int m=l+r>>1;
		if(check(m)) r=m;
		else l=m+1;
	}
	cout<<((r==n+1)?-1:l)<<endl;
	return 0;
}
2024/9/17 18:28
加载中...