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
(1≤T≤10),
which denotes the number of test cases.

For each test case, the first line of input contains single integer
n,L,R
(2≤n≤105,1≤L≤R≤1018).

The second line contains n
integers, the i-th
integer denotes ai
(1≤ai≤1018).

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

BestCoder 2nd Anniversary

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); 

	}
}
时间: 2024-09-30 10:59:58

HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)的相关文章

HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)

Palace Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 465    Accepted Submission(s): 118 Problem Description The last trial Venus imposes on Psyche is a quest to the underworld. She is to ta

HDU 5719 BestCoder 2nd Anniversary Arrange (DP)

Arrange Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 558    Accepted Submission(s): 198 Problem Description Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen

BestCoder 2nd Anniversary

http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+2nd+Anniversary&source=1&searchmode=source A 取最小的非零数,再相加 1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio>

BestCoder 2nd Anniversary的前两题

Oracle Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 41 Problem Description There is once a king and queen, rulers of an unnamed city, who have three daughters

hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719 题意:一个数列为1~N的排列,给定mn[1...n]和mx[1...n],问有符合的排列数为多少?如果不存在,输出0: 思路: 有解的几种条件: 1. mn , mx 变化单调: 2. mn,mx 不能同时变化: 3. 一个位置可选的个数>0; 当解存在时,递推出每次可选择的个数,num += mx[i] - mx[i-1] + mn[i-1] - mn[i] - 1; 即可: 坑:开始想成了

BestCoder 2nd Anniversary 1005&amp;Hdu 5722 -Jewelry

题意:问有多少个合法区间. 分析:对于[l,r],枚举右区间r,获取合法的l的区间.当增加一个元素Ai,原来合法的区间就会变不合法,要删掉,同时又会新增一个合法的区间,要插入. 例如,当x=2,对于元素 Ai其出现的位置为:1 2 3, 当新增位置4又出现Ai时,那么原来[1+1,2]的区间不合法,删掉.然后区间[2+1,3],插入. /************************************************ Author :DarkTong Created Time :

BestCoder 2nd Anniversary 1004&amp;Hdu 5721 Palace

题意:给定n个点,对于每次消失一个点,问剩下的点中的最短距离是多少,然后所有最短距离的和. 分析:1.模版题,然而没模版的我码了半天. 2.(1)只要不删掉与最短距离相关的两个点,那么最短距离不变. (2)若与最短距离的点相关,删掉点后,重新算一遍. /************************************************ Author :DarkTong Created Time :2016/7/18 14:01:14 File Name :d.cpp *******

BestCoder 2nd Anniversary 1001 Oracle

找到最小的非零数字拆开来相加. 高精度. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 #define LL long long 8 char s[10000005]; 9 int a[10000005],b[10000005]; 10

HDOJ 5720 Wool

Wool Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 647    Accepted Submission(s): 185 Problem Description At dawn, Venus sets a second task for Psyche. She is to cross a river and fetch gol