hdu 5720(贪心+区间合并)
Wool
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 347 Accepted Submission(s): 97
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.
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).
2 1 3
1 1
4 3 10
1 1 2 4
5
In the first example, $ 2, 3 $ are available.
In the second example, $ 6, 7, 8, 9, 10 $ are available.
#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(贪心+区间合并)的更多相关文章
- Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...
- I - Tunnel Warfare - hdu 1540(区间合并更新)
题意:在抗日战争期间,地道战在华北平原得到广泛的实施,一般而言,村庄通过一些隧道在一条线上连接,除了两端剩下的每个村庄都有两个相连. 侵略者会频繁的对这些村庄进行扫荡,并且摧他们的地道,当然八路军会把 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU 1540 Tunnel Warfare(线段树+区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- 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之间 ...
- hdu 3911 Black And White (线段树 区间合并)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...
- (线段树 区间合并更新)Tunnel Warfare --hdu --1540
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 4553 约会安排(线段树区间合并+双重标记)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 题目大意:就是有三种操作: ①DS x,安排一段长度为x的空闲时间跟屌丝一起,输出这段时间的起点 ...
随机推荐
- leetcode 【 Intersection of Two Linked Lists 】python 实现
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- iOS笔记057 - UI总结03
控制器的父子关系 1.控制器父子关系的建立原则 如果2个控制器的view是父子关系(不管是直接还是间接的父子关系),那么这2个控制器也应该为父子关系 [self.view addSubv ...
- 1024Studio官网
一.开发背景 在工作室成立之后,一直就想为工作室建设一个网站,这次乘着暑假有足够的空余时间,开始着手建设我们1024studio的官方网站. 二.系统设计 1.系统目标 根据网上查找的相关资料以及与工 ...
- CentOS下创建和root权限完全相同用户
新建用户 [root@bagon ~]# useradd newroot 修改密码 [root@bagon ~]# passwd newroot 编辑/etc/passwd,找到新建用户那一行 new ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- CodeForces C. Maximal Intersection
http://codeforces.com/contest/1029/problem/C You are given nn segments on a number line; each endpoi ...
- EF异常:对一个或多个实体的验证失败
try catch 捕获到错误.然后看.找到哪个是没填的..... 我是这种错误.
- java值转递?引用传递?
值传递是传递的是原值的副本,引用传递传递的是原值. 在Java中,如果是基本数据类型,传递的是该参数字面量值的拷贝.如果是引用数据类型,传递的是该参数所引用对象在堆中地址的拷贝. swap(int a ...
- npm & npm config
npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...
- hdu 1551 Cable master (二分法)
Cable master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...