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 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
a
i![]()
.
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.
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
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]
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≤10
5
,1≤L≤R≤10
18
)
.
The second line contains n
integers, the
i
-th integer denotes
a
i![]()
(1≤a
i
≤10
18
)
.
For each test case, the first line of input contains single integer n,L,R
The second line contains n
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 5HintIn the first example, $ 2, 3 $ are available. In the second example, $ 6, 7, 8, 9, 10 $ are available.
Source
题意大致是:给出已有木棍个数及长度 然后给出可用的木棍的长度范围 现在要再放一根木棍但是要使得任意三根木棍都不能组成三角形 问放的这根木棍的长度共有多少种选择
秉持着两边之和小于等于第三边不能构成三角形
/*
测试数据:
1000
4 1 20
5 6 7 8
4 1 150
2 3 7 100
2 1 1000000000000000000
2 3
4 1 1000000000000000000
2 3 7 100
4 1000000 1000000000000000
2 3 7 100
4 2 3
1 1 1 1
4 1 100
2 3 9 100
2 2 2
1 5
2 2 2
1 1
2 2 2
2 3
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100010
using namespace std;
long long a[maxn];
int main()
{
int t,n;
long long l,r,xl,xr,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d%I64d",&n,&l,&r);
for(int i=1;i<=n;++i)
scanf("%I64d",&a[i]);
ans=0;
sort(a+1,a+n+1);
xl=l>1?l:1;
xr=(a[2]-a[1])<=r?(a[2]-a[1]):r;
if(xl<=xr)
ans+=xr-xl+1;
for(int i=3;i<=n;++i)
{
if((a[i]-a[i-1])>=l&&(a[i-1]+1)<=r&&(a[i]-a[i-1])>=a[i-1]+a[i-2])
{
xl=l>a[i-1]+a[i-2]?l:(a[i-1]+a[i-2]);
xr=r<a[i]-a[i-1]?r:(a[i]-a[i-1]);
if(xl<=xr)
ans+=xr-xl+1;
}
}
if(a[n]+a[n-1]<=r)
{
if(a[n]+a[n-1]>=l)
ans+=r-(a[n]+a[n-1])+1;
else
ans+=r-l+1;
}
printf("%I64d\n",ans);
}
return 0;
}