hdu 5720 Wool(贪心,扫描线,区间合并)

本文探讨了一个涉及计算几何的问题,即在给定的木棍中找出不能构成三角形的新木棍的数量。通过对三角形边长关系的深入分析,提出了一种有效的算法解决方案,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wool

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


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.
题意:地上有n根木棍,现在给一个区间[L,R],问区间内有多少个数满足从地上任取两根木棍都不能组成三角形

思路:贴一发官方题解

考虑三角形三条边a,b,ca,b,c (a \ge b)(ab)的关系a - b < c, a + b > cab<c,a+b>c,即c \in (a-b,a+b)c(ab,a+b)

令加入的边为cc,枚举所有边作为aa的情况。对于所有可行的bb,显然与aa相差最小的可以让(a-b,a+b)(ab,a+b)覆盖范围最大,所以可以贪心地选择不大于aa的最大的bb

于是我们可以先将边按长度排序,然后a_iaia_{i + 1}ai+1建一条线段。线段并是不合法的部分。

将所有线段按左端点排序,按序扫描一遍,过程中统计答案即可。

时间复杂度O(Tn\ \log n)O(Tn logn)


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100050
long long a[N];
struct Inter
{
    long long l,r;
}p[N];
bool cmp(Inter a,Inter b)
{
    return a.l<b.l;
}
int main()
{
    int T,n;
    long long l,r;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %I64d %I64d",&n,&l,&r);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        sort(a+1,a+1+n);
        int cnt=0;
        long long maxn=-1;
        for(int i=2;i<=n;i++)
        {
            p[cnt].l=a[i]-a[i-1]+1;
            p[cnt++].r=a[i]+a[i-1]-1;
            maxn=max(maxn,p[cnt-1].r);
        }
        sort(p,p+cnt,cmp);
        int t=0;
        if(maxn<l||p[0].l>r)
        {
            printf("%lld\n",r-l+1);
            continue;
        }
        while(t<cnt&&p[t].r<l)
            t++;
        long long ll=max(p[t].l,l),rr=min(p[t].r,r);
        long long ans=rr-ll+1;
        for(int i=t+1;i<cnt;i++)
        {
            if(rr==r) break;
            if(p[i].l>r) break;
            if(p[i].r<=rr) continue;
            if(p[i].l<=rr) ll=rr+1;
            else ll=p[i].l;
            rr=p[i].r;
            rr=min(p[i].r,r);
            ans=ans+rr-ll+1;
        }
        printf("%lld\n",r-l+1-ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值