zoukankan      html  css  js  c++  java
  • hdu 5720 Wool

    hdu 5720

    问题描述
    黎明时,Venus为Psyche定下了第二个任务。她要渡过河,收集对岸绵羊身上的金羊毛。
    
    那些绵羊狂野不驯,所以Psyche一直往地上丢树枝来把它们吓走。地上现在有n n n根树枝,第i i i根树枝的长度是ai a_i ai​​.
    
    如果她丢的下一根树枝可以和某两根树枝形成三角形,绵羊就会被激怒而袭击她。
    
    现在Psyche手中只有长度不小于L L L且不大于R R R的树枝。请你帮忙计算,她下一根可以丢多少种不同长度的树枝而不会把绵羊激怒呢?
    输入描述
    第一行,一个整数T(1≤T≤10) T  (1 le T le 10) T(1T10),代表数据组数。
    
    对于每组数据,第一行有三个整数n,L,R n,L,R n,L,R (2≤n≤105,1≤L≤R≤1018) (2 le n le 10 ^ 5, 1 le L le R le 10 ^ {18}) (2n105​​,1LR1018​​)。
    
    第二行,n n n个整数,第i i i个整数为ai a_i ai​​ (1≤ai≤1018) (1 le a_i le 10 ^ {18}) (1ai​​1018​​),代表第i i i根树枝的长度。
    输出描述
    输出T T T行,对于每组数据,输出选取方式总数。
    输入样例
    2
    2 1 3
    1 1
    4 3 10
    1 1 2 4
    输出样例
    2
    5
    Hint
    对于第一组数据,可以选用长度为2,3 2, 3 2,3的树枝。
    
    对于第二组数据,可以选用长度为6,7,8,9,10 6, 7, 8, 9, 10 6,7,8,9,10的树枝。


    一般懒得解释题意就直接放题目!
    首先关于三角形的三边判断是任意一边要小于另外两边的和大于另外两边的差,所以就是找出范围就好。将地上的枝条排序,只要求出相邻的两两边的长度限制的范围就好,因为其他任意两两边的范围会被这个所包括
    然后将这些范围去重,形成几个互不相交的区间范围,然后用总的范围一个一个减去就好(当然,减得时候要判断这些范围与总的范围的相交
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 typedef long long ll;
     8 const int M = 1e5 + 10;
     9 ll a[M];
    10 
    11 struct node{
    12     bool dis;
    13     ll l,r;
    14 }b[M],c[M];
    15 
    16 bool cmp(node a,node b){return a.l<b.l;}
    17 
    18 bool judge(ll a,ll b,ll c)
    19 {
    20     if (a>=b&&a<=c) return true;
    21     return false;
    22 }
    23 
    24 int main()
    25 {
    26     int t,n;
    27     ll l,r;
    28     scanf("%d",&t);
    29     while (t--)
    30     {
    31         scanf("%d%I64d%I64d",&n,&l,&r);
    32         for (int i=1 ; i<=n ; i++)
    33             scanf("%I64d",&a[i]);
    34         sort(a+1,a+n+1);
    35         int ans=0;
    36         for (int i=2 ; i<=n ; i++)
    37         {
    38             b[++ans].l=a[i]-a[i-1]+1;
    39             b[ans].dis=false;
    40             b[ans].r=a[i]+a[i-1]-1;
    41             b[ans].dis=true;
    42         }
    43         sort(b+1,b+ans,cmp);
    44         int cas=0;c[++cas].l=b[1].l;c[cas].r=b[1].r;
    45         for (int i=2 ; i<=ans ; i++){
    46            if (judge(b[i].l,c[cas].l,c[cas].r)){
    47                if (!judge(b[i].r,c[cas].l,c[cas].r))
    48                   c[cas].r=b[i].r;
    49            }
    50            else{
    51               c[++cas].l=b[i].l;c[cas].r=b[i].r;
    52            }
    53         }
    54         ll res=r-l+1;
    55         for (int i=1 ; i<=cas ; i++){
    56             if (judge(c[i].l,l,r)){
    57                 if (judge(c[i].r,l,r))
    58                     res-=(c[i].r-c[i].l+1);
    59                 else
    60                     res-=(r-c[i].l+1);
    61             }
    62             else {
    63                 if (c[i].l<l){
    64                     if (judge(c[i].r,l,r))
    65                         res-=(c[i].r-l+1);
    66                     else if (c[i].r>r)
    67                         res-=(r-l+1);
    68                 }
    69             }
    70         }
    71         if (res<0) res=0;
    72         printf("%I64d
    ",res);
    73     }
    74     return 0;
    75 }
    View Code
    
    
    
    关系)
  • 相关阅读:
    项目冲刺-第一天
    第四次个人作业-关于微软必应词典客户端 的案例分析
    第三次作业-结对编程
    学院教师开课管理网站项目
    使用Git进行代码管理的心得--github for windows
    十天冲刺---Day8
    十天冲刺---Day7
    十天冲刺---Day6
    十天冲刺---Day5
    十天冲刺---Day4
  • 原文地址:https://www.cnblogs.com/JJCHEHEDA/p/5680774.html
Copyright © 2011-2022 走看看