hdu 5720 Wool

1003 Wool

Provider : frank_c1

考虑三角形三条边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)

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


 

在遍历线段的时候,有两种选择:

1. 记录线段覆盖了多少个点,最后用小孩手中到总点数减掉被覆盖到点数就是答案。但是这样要考虑到事情比较多:

 2.先假设所有点都没有被覆盖,然后从小孩整个线段上逐步减掉被覆盖到部分。

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.
 
 
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxx=100005;
struct Node{
    long long l;
    long long r;
    long long len;
}edg[maxx];
long long  a[maxx];
bool cmp(Node a,Node b){
        return a.l<b.l;
}
int main(){
   int t;
   scanf("%d",&t);
   while(t--){
       int n;
       long long l,r;
       scanf("%d%lld%lld",&n,&l,&r);
       for(int i=0;i<n;i++){
           scanf("%lld",&a[i]);
       }
       sort(a,a+n);
       for(int i=1;i<n;i++){
           edg[i-1].l=a[i]-a[i-1];
           edg[i-1].r=a[i]+a[i-1];
       }
       long long ml,mr;
       sort(edg,edg+n-1,cmp);
       ml=l;
       long long ans=0;
       for(int i=0;i<n-1&&edg[i].l<=r;i++){
           if(edg[i].l>=ml){
               ans+=edg[i].l-ml+1;
           }
           ml=max(ml,edg[i].r);
       }
       ans+=max((long long )0,r+1-ml);
       printf("%lld\n",ans);
   }
   return 0;
}

 

1003 Wool

Provider : frank_c1

考虑三角形三条边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)

posted @ 2016-07-18 21:03  超级学渣渣  阅读(144)  评论(0编辑  收藏  举报