HDU 5720 Wool(排序 遍历)

Wool

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


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  5718  5717  5716 
 


题目链接:HDU 5720 Wool


题目描述:

地上有n根木棍,求(L,R)范围内有多少个整数满足与n根木棍中的任意两根都不可能组成三角形。

第一行T(1<=T<=10)表示测试的组数,每组数据两行。第一行三个数n(2,10^5) L(1,10^18) R(1,10^18),第二行n个数(1,10^18)。

解题思路:

本来拿到这题也是没有思路,赛后看了有大神ac的代码果然还是厉害。感觉想法很独特。

地上的树枝原本的数据可能会形成三角形,对结果不影响。先将地上的树枝按长度大小进行排序,然后从大到小遍历。每次选取两个最大他们的和就是能达到的最大的。在遍历过程中不断缩小R的值,保持L的值不变。在遍历过程中从(L,R)通过改变R中删除不满足条件的点,或删除已经计算过的点。最后如果L<=R则将R-L+1加到结果ans中。


HDU官方题解:



附AC代码:

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;
typedef long long LL;

LL a[100015];

int main()
{
    int T;
    cin >> T;
    int n;
    LL L,R;
    while(T--)
    {
        cin >> n;
        cin >> L >> R;
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        sort(a+1,a+n+1);//对地上的树枝进行排序
        LL ans=0;//初始答案为0
        for(int i=n;i>=2;i--)//从最大向小遍历
        {
            LL st=a[i]+a[i-1];//记录当前树枝的上界
            st=max(st,L);
            if(st<=R)
            //如果当前数值所能达到的最大值小于等于R则st-R之间的值都满足条件
            {
                ans+=R-st+1;
                R=a[i]-a[i-1];//改变右界的值
            }
            else//如果当前数值所能达到的最大值大于R
            {
                R=min(R,a[i]-a[i-1]);//右界的值可能会改变
            }
        }
        if(L<=R)
            ans+=(R-L+1);
            //当遍历完一遍的时候如果所给区间还有满足条件的值,则赋给结果
        cout << ans << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值