HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)

Wool

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 686    Accepted Submission(s): 192

Problem Description
At dawn, Venus sets a second task for Psyche.
She is to cross a river and fetch golden wool from violent sheep who graze on the other side.
The sheep are wild and tameless, so Psyche keeps on throwing sticks to keep them away.
There are n sticks on the ground, the length of the i -th stick is ai .
If the new stick she throws forms a triangle with any two sticks on the ground, the sheep will be irritated and attack her.
Psyche wants to throw a new stick whose length is within the interval [L,R] . Help her calculate the number of valid sticks she can throw next time.

Input
The first line of input contains an integer T (1T10) , which denotes the number of test cases.
For each test case, the first line of input contains single integer n,L,R (2n105,1LR1018) .
The second line contains n integers, the i -th integer denotes ai (1ai1018) .
Output
For each test case, print the number of ways to throw a stick.

Sample Input
  
  
2 2 1 3 1 1 4 3 10 1 1 2 4
Sample Output
  
  
2 5
Hint
In the first example, $ 2, 3 $ are available. In the second example, $ 6, 7, 8, 9, 10 $ are available.
Source
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5722 5721 5717 5716 5715 
 
题意:我做这道题时WA了很多发,发现是想太多了。

给你n个数和L,R。问你在【L,R】的区间内有多少个数不能和a数组中的任意两个数构成三角形。


题解:首先给n个数sort一下,你会发现,在这n个数中,最不能取的且最大的数就是a[n](n从1开始)。对于i:1~n-1中,最不能取的区间就是【a[i+1]-a[i]+1,a[i+1]+a[i]-1】,枚举这个区间的i:1~n-1,再和【L,R】相比,取其相对于【L,R】的补集即可。

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
typedef long long LL;
typedef unsigned long long uLL;
const int N=100010;
LL a[N]; 
int main()
{
	int n,t;
	LL R ,L;
	LL ans;
	scanf("%d",&t);
	while(t--)
	{
		ans=0;
		scanf("%d%I64d%I64d",&n,&L,&R);
		for(LL i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
		std::sort(a,a+n+1);
		/*
		LL res=a[n]+a[n-1];
		LL sd=std::max(L,res);
		ans=R-sd+1;
		*/
		LL l,r;
		for(LL i=n-1;i>=1;--i)
		{    
		   
			 l=a[i+1]-a[i]+1; 
			 r=a[i+1]+a[i]-1;
			if(l>R||r<L)continue;
			if(R>r) ans+=R-r;
			R=l-1;
			if(L>R)break; 
		}
		if(L<=R) ans+=R-L+1;
		
		printf("%I64d\n",ans); 
	
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值