uva1617

题意:有n条长度为1的线段,现给出它们的区间,找出它们的位置,使得它们间的空隙数目最小

分析:贪心,按r,l进行排序,从左向右找,每次都取最右端,若r大于当前最后一个就可以放在r+1,r等于当前,那么当前的可以放在r-1的位置,r就放在r的位置,l大于当前最后一个,那么表示不相交,肯定有一个空隙

#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
struct node {
	int l, r;
	bool operator<(const node &u) {
		return r == u.r ?  l < u.l :  r < u.r;
	}
}p[100000+10];
int main() {

	int kase,n;

	cin >> kase;
	while (kase--) {
		cin >> n;
		for (int i = 0; i < n; i++)cin >> p[i].l >> p[i].r;
		sort(p, p + n);
		int ant = p[0].r;//当前取长度1的线段的最后一个结点的r
		int ans = 0;
		for (int i = 0; i < n; i++) {
			if (p[i].r == ant)continue;
			if (p[i].r > ant&&p[i].l <= ant) {
				ant++;
			}
			else if (p[i].l > ant) {
				ans++;
				ant = p[i].r;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值