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. 内存泄漏分析 mat 使用 activity泄漏

    https://github.com/square/leakcanary square 公司出品 mat 下载地址: http://pan.baidu.com/s/1kVPoIxx 两天,一个内存泄漏 ...

  2. 如何使用API创建OpenStack虚拟机?

    在安装时OpenStack会加载配置信息.有不同的虚拟机模板而且与在Amazon EC2以及其他平台上看到的完全一样.这些配置是内存.vCPU.磁盘容量等的组合,定义了虚拟机的大小及容量.可以使用如下 ...

  3. VSX-4 VSXTra

    要介绍VSXTra项目不是一个简单的事情. 而且要在上面进行扩展,删减就更不容易. 源码分析的资料几乎没有,可怜的几个示例项目,相较而言,英文已经不是阻碍我前进的步伐了. 本篇只是简单的分析,对于已经 ...

  4. runloop的mode作用是什么?

    用来控制一些特殊操作只能在指定模式下运行,一般可以通过指定操作的运行mode来控制执行时机,以提高用户体验 系统默认注册了5个Mode kCFRunLoopDefaultMode:App的默认Mode ...

  5. 《Cracking the Coding Interview》——第14章:Java——题目1

    2014-04-26 18:20 题目:从继承的角度,把构造函数设成private有什么意义? 解法:就不能继承了.单体模式里也这么干,目的是为了不让使用者自主生成对象,进行限制. 代码: // 14 ...

  6. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  7. elk-logstash: window下指定jdk目录

    \bin\logstash.bat文件中, SETLOCAL的后面,CALL "%SCRIPT_DIR%\setup.bat" 的前面增加一行: @echo off SETLOCA ...

  8. postman导出excel出现response

    https://jingyan.baidu.com/article/915fc414559b4351394b2084.html

  9. jekens介绍及服务搭建

    https://blog.csdn.net/achuo/article/details/51086599 https://blog.csdn.net/qq_37372007/article/detai ...

  10. 孤荷凌寒自学python那些事第二天

    孤荷凌寒自学python第二天 Python的变量声明 (完整学习过程屏幕记录视频在文末,手写笔记在文末) Python的变量声明不必要显式指定变量类型 甚至也不需要进行显式的声明 比javascri ...