2019杭电多校 HDU-6695 贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6695

题解:分成两组,其中一组最大的x - 另一组最大的y 绝对值最小

题解:按照x从小到大排序,枚举最大x,比x大的都放到另一组,每处理完y记录一下,然后从前面找到大于x和小于x的最符合的,判断一下是否可取,然后就是很多x相等的,找一个y最符合的

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100100;
int n;
struct node {
	ll x, y; 
	bool operator <(const node &b) const {
		return x < b.x;
	}
}a[N];
ll maxx[N];
int main() {
	int T;
	
	ll ans, cnt, cnt1, cnt2, tmp;
	scanf("%d", &T);
	while(T--) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			scanf("%lld %lld", &a[i].x, &a[i].y);
		sort(a + 1, a + 1 + n);
		maxx[n + 1] = 0;
		for(int i = n; i >= 1; i--) maxx[i] = max(a[i].y, maxx[i + 1]);
		set<ll> s;
		set<ll>::iterator it;
		ans = 1e18;
		for(int i = 1, j = 1, k; i <= n;) {
			
			cnt = maxx[i + 1];
			tmp = abs(cnt - a[i].x);
			cnt1 = -1;
			cnt2 = -1;
			it = s.lower_bound(a[i].x);
			if(i != 1) {
				if(it != s.begin()) {
					it--;
					cnt1 = *it;
				}
			}
			it = s.lower_bound(a[i].x);
			if(it != s.end()) {
				cnt2 = *it;
			}
			if(cnt1 != -1 && cnt1 > cnt) tmp = min(tmp, abs(cnt1 - a[i].x));
			if(cnt2 != -1 && cnt2 > cnt) tmp = min(tmp, abs(cnt2 - a[i].x));
			j = i + 1;
			s.insert(a[i].y);
			if(i + 1 <= n && a[i].x == a[i + 1].x) {
				
				k = i + 1;
				while(k <= n && a[i].x == a[k].x) k++;
				j = i;
				while(j < k ) {
					
					if(a[j].y >= maxx[k]) tmp = min(tmp, abs(a[i].x - a[j].y));
					s.insert(a[j].y);
					j++;
				}
				
			} 
			i = j;
			
			ans = min(ans , tmp); 
			
		}
		printf("%lld\n", ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值