hdu 5720(贪心)

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

官方题解:

考虑三角形三条边 a,b,c  (ab)  的关系 ab<c,a+b>c  ,即 c(ab,a+b)  。令加入的边为 c  ,枚举所有边作为 a  的情况。对于所有可行的 b  ,显然与 a  相差最小的可以让 (ab,a+b)  覆盖范围最大,所以可以贪心地选择不大于 a  的最大的 b  。于是我们可以先将边按长度排序,然后 a i   a i+1   建一条线段。线段并是不合法的部分。将所有线段按左端点排序,按序扫描一遍,过程中统计答案即可。时间复杂度 O(Tn logn)  。  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL,LL> Pi;
LL x[100005];
Pi pr[100005];

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		LL L,R;
		scanf("%d%lld%lld",&n,&L,&R);
		for(int i = 0; i < n; ++i) scanf("%lld",&x[i]);
		sort(x,x+n);
		for(int i = 0; i < n-1; ++i)
		{
			pr[i].first = x[i+1]-x[i];
			pr[i].second = x[i+1]+x[i];
		}
		sort(pr,pr+n-1);
		LL ans = 0;
		LL mx = L;
		for(int i = 0; i < n-1 && pr[i].first <= R; ++i)
		{
			if(pr[i].first >= mx) ans += (pr[i].first-mx+1);
			mx = max(mx,pr[i].second);
		}
		ans += max(0LL,R+1-mx);
		printf("%lld\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值