HDU 5720 Wool

来源:互联网 发布:java package类 编辑:程序博客网 时间:2024/04/29 05:41

题目链接:HDU5720

Wool

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


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
22 1 31 14 3 101 1 2 4
 

Sample Output
25
Hint
In the first example, $ 2, 3 $ are available.In the second example, $ 6, 7, 8, 9, 10 $ are available.
 
题意:地上有一堆线段,现在要扔下一个长度在L和R之间的线段,要求这个线段不能跟地上的任意2个线段构成三角形,问一共有多少种方案。

题目分析:首先排序是必要的,从小到大排序。由于若a,b已经固定(a>=b),c<=a-b||c>=a+b才能保证不会构成三角形。对于排好序的数列a[0~n-1]中任意一个数a[i]而言,a[i+1]是可以构成最小的解空间来满足所有的情况。所以从左枚举a[i],并求出不可取的线段范围求并,其间统计答案,复杂度O(nlogn)。

比赛过程中是对所有可取空间求交集,结果一直在WA。最后5分钟才发现应该对不可取空间求并,改了7分钟在杭电上1A了。

////  main.cpp//  Wool////  Created by teddywang on 2016/7/17.//  Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;long long  int a[100019];int main(){    long long int T,L,R,l,r;    int n;    cin>>T;    while(T--)    {        memset(a,0,sizeof(a));        cin>>n>>L>>R;        for(int i=0;i<n;i++)        {            scanf("%lld",&a[i]);        }        sort(a,a+n);        long long int ans=0;        for(int i=n-1;i>=1;i--)        {            l=a[i]-a[i-1];            r=a[i]+a[i-1];            if(l>R||r<L) continue;            long long int buf=R-r+1;            if(buf<0) buf=0;            ans+=buf;            R=min(l,R);            if(R<L) break;        }        long long int buf=R-L+1;        if(buf<0) buf=0;        ans+=buf;        cout<<ans<<endl;    }}


0 0
原创粉丝点击