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的空闲时间跟屌丝一起,输出这段时间的起点 ...
随机推荐
- Java Spring Controller 获取请求参数的几种方法
技术交流群:233513714 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...
- 部署微软Nano Server的好处是什么?
虚拟化对数据中心效率和硬件利用率产生了戏剧性的影响,但是接下来有关系统整合的主要责任落 在了操作系统的重量上.虚拟机通常运行企业级操作系统,比如Windows Server,但是Windows Ser ...
- 腾讯课堂之前端开发html5css3javascriptjQueryJS年薪20万
第一章 网页制作零基础 第一节 什么是HTML 第二节 HTML基本语法 第三节 HTML结构标签 第四节 HTML常用标签及属性 第五节 HTML无序列表UL标签 第六节 HTML定义列表DL标签 ...
- Visual Studio 2017 的 JavaScript 调试功能的关闭
关闭方法其实很简单,Options => Debugging => General => Enable JavaScript debugging for ASP.NET (Chrom ...
- 《Cracking the Coding Interview》——第2章:链表——题目3
2014-03-18 02:25 题目:给定一个单链表中间的节点,删掉那个节点. 解法:把后面节点的数据域拷到当前节点来,然后删除后面那个节点.当前节点不是尾巴,所以后面不为空. 代码: // 2.2 ...
- PEAR DB 初学笔记
1.数据查询 i. DB_common::getAll() DB_FETCHMODE_ORDERED . DB_FETCHMODE_ASSOC . DB_FETCHMODE_OBJECT ii. DB ...
- Limeng:Individual Project: Word frequency program -BUAA Advanced Software Engineering
11061190-李孟 Implement a console application to tally the frequency of words under a directory (2 mod ...
- JSP/Servlet Web 学习笔记 DayThree —— 实现一个登陆小界面
项目说明 利用JSP.HTML.JS实现了一个简易的登陆系统.根据前两天的所学,实现了如下功能: a)用户名.密码验证(不基于数据库,只做一个简单的表单数据获取并验证) b)页面访问次数统计 c)验证 ...
- free、vmstat监视内存使用情况
9. free 查询可用内存 free工具用来查看系统可用内存: /opt/app/tdev1$free total used free shared buffers cached Mem: 8175 ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...