求助,悬关
查看原帖
求助,悬关
668886
fishing_cat楼主2024/7/10 20:32
#include<bits/stdc++.h>
using namespace std;
void read(int &x){
	x = 0;
	char c = getchar();
	int f = 0;
	for(;!isdigit(c);c = getchar())
		f |= c == '-';
	for(;isdigit(c);c = getchar())
		x = x*10 + (c ^ '0');
	if(f) x = -x;
}
struct f{
	int x;
	int y;
}site[100010];
int cmp(f a,f b){
	return a.x < b.x;
}
deque <int> mx,mn;
int n,d,ans = 0x3f3f3f3f;
int main(){
	read(n);read(d);
	for(int i=1;i<=n;i++){
		read(site[i].x);
		read(site[i].y);
	}
	sort(site+1,site+n+1,cmp);
	for(int i=0,r=0;i<=n;i++){
		while(!mx.empty() && mx.front()<i)mx.pop_front();
		while(!mn.empty() && mn.front()<i)mn.pop_front();
		if(mx.empty())mx.push_front(0);
		if(mn.empty())mn.push_front(0);
		while(site[mx.front()].y - site[mn.front()].y < d && r < n){
			r++;
			while(!mx.empty() && site[mx.back()].y < site[r].y)
				mx.pop_back();
			mx.push_back(r);
			while(!mn.empty() && site[mn.back()].y > site[r].y)
				mn.pop_back();
			mn.push_back(r);
		}
		if(r != n)
			ans = min(ans,site[r].x - site[i].x);
	}
	if(ans >= 0x3f3f3f3f)printf("-1\n");
	else printf("%d\n",ans);
	return 0;
}

WA 了第三个点,求助

2024/7/10 20:32
加载中...