Wool

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

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 (1≤T≤10), which denotes the number of test cases.

For each test case, the first line of input contains single integer n,L,R (2≤n≤105,1≤L≤R≤1018).

The second line contains n integers, the i-th integer denotes ai (1≤ai≤1018).

 
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
 
题意:给出n,L,R,表示地上现在有n根树枝,你手里现有[L,R]长度的树枝各一根,要求选择一根树枝,不能与地上任意两根树枝构成三角形,问有多少种选法。
题解:假设当前的三角形三边分别为 a,b,c (a>=b) 那么 相对于a 能够扩展的范围就是 (a-b,a+b) 当 b尽量逼近 a 的时候扩展的范围最大,于是排序之后求出每个点管辖区间的范围,然后进行区间合并,一定要记得讨论区间边界。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = ;
typedef long long LL;
const LL INF = ;
LL a[N];
bool HASH[N];
struct Node{
LL l,r;
}node[N];
int cmp(Node a,Node b){
return a.l<b.l;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int n;
LL l,r;
scanf("%d%lld%lld",&n,&l,&r);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+,a+n+);
LL MIN = INF,MAX = -;
int id = ;
for(int i=;i<=n;i++){
LL k = a[i]-a[i-]+,t = a[i]+a[i-]-;
if(k>=l&&t<=r){
node[id].l = k;
node[id++].r = t;
}else if(k>=l&&t>=r){
if(k>r) continue;
if(k<=r){
node[id].l = k;
node[id++].r = r;
}
}else if(k<=l&&t<=r){
if(t<l) continue;
if(t>=l){
node[id].l = l;
node[id++].r = t;
}
}else if(k<=l&&t>=r){
node[id].l = l;
node[id++].r = r;
}
}
if(id==){
printf("%lld\n",r-l+);
continue;
}
LL cnt = ;
sort(node+,node+id,cmp);
LL start=node[].l,pend = node[].r;
for(int i=;i<id;i++){
if(node[i].l>pend){
cnt+=pend-start+;
start = node[i].l;
pend = node[i].r;
}else pend = max(node[i].r,pend);
}
cnt+=pend-start+;
printf("%lld\n",r-l+-cnt);
}
return ;
}

hdu 5720(贪心+区间合并)的更多相关文章

  1. Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】

    任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...

  2. I - Tunnel Warfare - hdu 1540(区间合并更新)

    题意:在抗日战争期间,地道战在华北平原得到广泛的实施,一般而言,村庄通过一些隧道在一条线上连接,除了两端剩下的每个村庄都有两个相连. 侵略者会频繁的对这些村庄进行扫荡,并且摧他们的地道,当然八路军会把 ...

  3. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  4. HDU 1540 Tunnel Warfare(线段树+区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...

  5. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  6. HDU 3911 Black And White (线段树区间合并 + lazy标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 给你n个数0和1,m个操作: 0操作  输出l到r之间最长的连续1的个数 1操作  将l到r之间 ...

  7. hdu 3911 Black And White (线段树 区间合并)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...

  8. (线段树 区间合并更新)Tunnel Warfare --hdu --1540

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. HDU 4553 约会安排(线段树区间合并+双重标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 题目大意:就是有三种操作: ①DS x,安排一段长度为x的空闲时间跟屌丝一起,输出这段时间的起点 ...

随机推荐

  1. Oracle 11g数据库安装与卸载的方法图解(windows)

    一.Oracle 11g安装 安装之前要先确定自己的电脑配置,以windows为例,如果是win7以下系统如xp等,可以选择Oracle 10g.因为10g的程序文件只有200多兆,而11g及达到了2 ...

  2. Lambda与LINQ

    Lambda与LINQ写法对比: 上为Lambda 下为LINQ 显示指定列 Students.select(u=>(new {Name=u.Sname,Address=u.Saddress}) ...

  3. 部署 CA 和 NPS 服务器证书

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 按类别 ...

  4. C#中窗体、MDI的使用心得

    1、Show()和ShowDialog()的差别,前者在执行之后,无需等待返回,多用于MDI窗体的显示(MDI窗体之间是并列的,可以随时切换,需要注意自己调整的页面的需求,是要并列,还是要等待其中一个 ...

  5. USACO Section2.1 Ordered Fractions 解题报告

    frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  6. python的inspect模块

    一.type and members 1. inspect.getmembers(object[, predicate]) 第二个参数通常可以根据需要调用如下16个方法: 返回值为object的所有成 ...

  7. hdu 1714 RedField

    RedField Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. myEclipse如何将程序部署到tomcat(附录MyEclipse调试快捷键)

    部署 1.选中你要部署的项目,在工具栏找到 Deploy MyEclipse J2EE Project to Server 2.单击Add,即出现如下界面.选择相应的Server,要和你在配置tomc ...

  9. PHP命名空间与use

    当在一个大型项目很多程序员书写模板时,最怕出现的问题就是命名,如果一个PHP脚本出现了同名的类或者方法,就会报错(fatal error),使用命名空间可以 解决这个问题 知识点: 命名空间names ...

  10. input file request.files[] 为空

    需要在 form 里设置 一句话 :  $('form').attr("enctype", "multipart/form-data"); <form e ...