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. FastJson 打Release 包解析失败

    debug 的时候,fastJson 解析数据正常.但是打了release 的时候,解析的List 总是null. 找了半天,发现,是fastJson 是对泛型有问题. 解决办法: -keepattr ...

  2. Android toolbar menu 字体点击样式

    今天在做toolbar的时候,右边的菜单的点击事件,就是文字,然后文字的样式,文字的大小,文字的颜色,高了半天.最后发现,文字点下去之后是有样式的,也就是按下去有阴影. 哥哥的耐心好,就知道这不是问题 ...

  3. 【转】android makefile文件分析

    Makefile的规则如下: target ... : prerequisites ... command ... ... target可以是一个目标文件,也可以是Object File(例如hell ...

  4. Python 3基础教程5-while循环语句

    本文开始介绍循环语句,和其他编程语言一样,Python中有while循环和for循环,这里介绍while循环. 语法: while 条件表达式为真: 做一些事情 实际生活中有很多这样的循环场景,这里举 ...

  5. express常用代码片段

    请求模块: var express = require('express'); var router = express.Router(); // 拿到express框架的路由 var mongoos ...

  6. Java Web Action DAO Service层次理解

    参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...

  7. Mysql 查询—按位运算

    前言:虽说这是件小事儿,但本宝宝思前想后,还是为它留下一笔,嘿嘿.反正写博客不浪费纸和笔!好久没有开启我的逗比模式了,我亲爱的乖徒弟DBA,DBB,DBAA等,好久不见你们,遥祝幸福快乐+DB. 整个 ...

  8. sqlachemy 原生sql输出

    在创建引擎时,将echo参数配置成True,会输出sql执行语句记录.默认False create_engine(statsticConf.sqlalchemy_mysql,connect_args= ...

  9. rsync同步数据

    1. rsync 命令格式rsync [OPTION]... SRC DESTrsync [OPTION]... SRC [USER@]HOST:DESTrsync [OPTION]... [USER ...

  10. 如何出发匿名映射呀【log】

    malloc-9711 [002] .... 40794.642938: mm_vmscan_lru_shrink_inactive: nid=0 zid=1 nr_scanned=3 nr_recl ...