zoukankan      html  css  js  c++  java
  • hdu-5720 Wool(区间并+扫描线)

    题目链接:

    Wool

    Time Limit: 8000/4000 MS (Java/Others)   

     Memory Limit: 262144/262144 K (Java/Others)


    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 (2n10^5,1LR10^18).

    The second line contains n integers, the i-th integer denotes ai (1ai10^18).
     
    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
     
    题意:
     
    已经由n跟长为ai的线段,现在给定区间[l,r],问这个区间里还有多少个数与那一堆数不会形成三角形;
     
    思路:
     
    把a数组排序,i<j;选a[i]和a[j],(a[i]-a[j],a[i]+a[j]);这是不能出现的数的区间,可知(a[i]-a[i-1],a[i]+a[i-1])是选a[i]这个数会出现的最大范围的区间;
    所以就形成了n-1个区间[a[i]-a[i-1]+1,a[i]+a[i-1]-1],把这些区间扫一遍和并后求出覆盖了[l,r]中的多少个点,然后用[l,r]的点数减去就是答案了;
     
    AC代码:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=998244353;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e5+10;
    const int maxn=1e3+10;
    const double eps=1e-6;
    
    int n;
    LL l,r,a[N];
    struct node
    {
        LL l,r;
    }po[N];
    int cmp(node x,node y)
    {
        if(x.l==y.l)return x.r<y.r;
        return x.l<y.l;
    }
    int vis[N];
    int main()
    {
            int t;
            read(t);
            while(t--)
            {
                mst(vis,0);
                read(n);read(l);read(r);
                For(i,0,n-1)read(a[i]);
                sort(a,a+n);
                For(i,1,n-1)
                {
                    po[i].r=a[i]+a[i-1]-1;
                    po[i].l=a[i]-a[i-1]+1;
                }
                sort(po+1,po+n,cmp);
                LL len=0;
                For(i,1,n-1)
                {
                    if(i==n-1)continue;
                    if(po[i].r<po[i+1].l)continue;
                    else 
                    {
                        po[i+1].l=po[i].l;
                        po[i+1].r=max(po[i].r,po[i+1].r);
                        vis[i]=1;
                    }
                }
                For(i,1,n)
                {
                    if(!vis[i])
                    {
                        if(po[i].l>r||po[i].r<l||po[i].l>po[i].r)continue;
                        LL L=max(po[i].l,l),R=min(po[i].r,r);
                        len=len+(R-L+1);
                    }
                }
                cout<<r-l+1-len<<"
    ";
            }
            
            return 0;
    }
    

      

  • 相关阅读:
    Entity Framework 程序设计入门二 对数据进行CRUD操作和查询
    LLBL Gen + Entity Framework 程序设计入门
    企业管理软件开发之九 以数据绑定为基础的控件只读,创建时可写,必须大写,必须小写的原理与实现
    企业管理软件开发之八 多国语言功能设计与实现
    解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
    实用的开放源码的Excel导入导出类库 CarlosAg ExcelXmlWriter
    .NET程序集强命名删除与再签名技术 源代码剖析
    ibatis.net:第七天,QueryWithRowDelegate
    ibatis.net:第六天,QueryForList
    ibatis.net:第五天,QueryForObject
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5680181.html
Copyright © 2011-2022 走看看