HDU - 6695

题目链接:HDU - 6695


我们先按照x排序,然后依次枚举最大的x是多少。

我们可以发现,x 之前的随意选取都是对 x 的最大值无影响的,然后当前的最优解可以是,后面未选择的最大的 y 和当前 x 的差值,如果 y 小于当前的 x ,那么可以从 x 之前的找一个接近的 y 。

这个用multiset维护即可。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,res,mx[N];	multiset<int> s;
struct node{int x,y;}t[N];
inline void solve(){
	cin>>n; s.clear(); res=1e18;
	for(int i=1;i<=n;i++)	scanf("%lld %lld",&t[i].x,&t[i].y);
	sort(t+1,t+1+n,[](node a,node b){return a.x<b.x;}); mx[n+1]=-1e18;
	for(int i=n;i>=1;i--)	mx[i]=max(mx[i+1],t[i].y);
	for(int i=1;i<=n;i++){
		res=min(res,abs(mx[i+1]-t[i].x));
		if(mx[i+1]<t[i].x){
			auto pos=s.lower_bound(t[i].x);
			if(pos!=s.end())	res=min(res,*pos-t[i].x);
			if(pos!=s.begin())	res=min(res,t[i].x-(*--pos));
		}
		s.insert(t[i].y);
	}
	printf("%lld\n",res);
}
signed main(){
	int T; cin>>T; while(T--) solve();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/107601303
hdu