hdu 5720 Wool
黎明时,Venus为Psyche定下了第二个任务。她要渡过河,收集对岸绵羊身上的金羊毛。 那些绵羊狂野不驯,所以Psyche一直往地上丢树枝来把它们吓走。地上现在有n n n根树枝,第i i i根树枝的长度是ai a_i ai. 如果她丢的下一根树枝可以和某两根树枝形成三角形,绵羊就会被激怒而袭击她。 现在Psyche手中只有长度不小于L L L且不大于R R R的树枝。请你帮忙计算,她下一根可以丢多少种不同长度的树枝而不会把绵羊激怒呢?
第一行,一个整数T(1≤T≤10) T (1 \le T \le 10) T(1≤T≤10),代表数据组数。
对于每组数据,第一行有三个整数n,L,R n,L,R n,L,R (2≤n≤105,1≤L≤R≤1018) (2 \le n \le 10 ^ 5, 1 \le L \le R \le 10 ^ {18}) (2≤n≤105,1≤L≤R≤1018)。
第二行,n n n个整数,第i i i个整数为ai a_i ai (1≤ai≤1018) (1 \le a_i \le 10 ^ {18}) (1≤ai≤1018),代表第i i i根树枝的长度。
输出T T T行,对于每组数据,输出选取方式总数。
2
2 1 3
1 1
4 3 10
1 1 2 4
2
5
对于第一组数据,可以选用长度为2,3 2, 3 2,3的树枝。 对于第二组数据,可以选用长度为6,7,8,9,10 6, 7, 8, 9, 10 6,7,8,9,10的树枝。 一般懒得解释题意就直接放题目!
首先关于三角形的三边判断是任意一边要小于另外两边的和大于另外两边的差,所以就是找出范围就好。将地上的枝条排序,只要求出相邻的两两边的长度限制的范围就好,因为其他任意两两边的范围会被这个所包括
然后将这些范围去重,形成几个互不相交的区间范围,然后用总的范围一个一个减去就好(当然,减得时候要判断这些范围与总的范围的相交
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
const int M = 1e5 + ;
ll a[M]; struct node{
bool dis;
ll l,r;
}b[M],c[M]; bool cmp(node a,node b){return a.l<b.l;} bool judge(ll a,ll b,ll c)
{
if (a>=b&&a<=c) return true;
return false;
} int main()
{
int t,n;
ll l,r;
scanf("%d",&t);
while (t--)
{
scanf("%d%I64d%I64d",&n,&l,&r);
for (int i= ; i<=n ; i++)
scanf("%I64d",&a[i]);
sort(a+,a+n+);
int ans=;
for (int i= ; i<=n ; i++)
{
b[++ans].l=a[i]-a[i-]+;
b[ans].dis=false;
b[ans].r=a[i]+a[i-]-;
b[ans].dis=true;
}
sort(b+,b+ans,cmp);
int cas=;c[++cas].l=b[].l;c[cas].r=b[].r;
for (int i= ; i<=ans ; i++){
if (judge(b[i].l,c[cas].l,c[cas].r)){
if (!judge(b[i].r,c[cas].l,c[cas].r))
c[cas].r=b[i].r;
}
else{
c[++cas].l=b[i].l;c[cas].r=b[i].r;
}
}
ll res=r-l+;
for (int i= ; i<=cas ; i++){
if (judge(c[i].l,l,r)){
if (judge(c[i].r,l,r))
res-=(c[i].r-c[i].l+);
else
res-=(r-c[i].l+);
}
else {
if (c[i].l<l){
if (judge(c[i].r,l,r))
res-=(c[i].r-l+);
else if (c[i].r>r)
res-=(r-l+);
}
}
}
if (res<) res=;
printf("%I64d\n",res);
}
return ;
}
关系)
hdu 5720 Wool的更多相关文章
- hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5720 题意:有n(n <= 105)个数 ,每个数小于等于 1018:问在给定的[L,R]区间中 ...
- hdu 5920 Wool 思路
Wool Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem D ...
- hdu 5720(贪心+区间合并)
Wool Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- hdu 5720
考虑三个树枝:a,b,c若c是将要抛出的树枝,那么形成三角形的条件是a+b>c and a-b<c 可以写成 c属于开区间(a-b,a+b)对于每个C和许许多多的其他边,如何保证C不构成三 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- AS2使用ExternalInterface
以下代码是帧脚本(选中某帧F9,粘贴) import flash.external.ExternalInterface; // 假的,目的是为了执行createButton里面的ExternalInt ...
- 12 Linux下crontab详解
1. 概述: crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进 ...
- ASP.NET学习笔记1—— MVC
MVC项目文件夹说明 1.App_Data:用来保存数据文件 2.App_Start:包含ASP.NET-MVC系统启动的相关类文件 3.Controllers:存放整个项目"控制器&quo ...
- ios中strong和weak的解释理解
来自stackoverflow解释的挺有意思的 Imagine our object is a dog, and that the dog wants to run away (be dealloca ...
- 移动端line-height失效
移动端高度过小,使用rem布局时div里面的文字不能用line-height垂直居中: 解决方案,先高度,字体大小扩大n倍,然后利用transform:scale(0.n)缩小即可.
- OSX 10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods
10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods
- union select
union select 联合查询 ,即合并(取交集,结果中没有重复行)前后两个查询:前提是前后查询视图必须拥有相同数量的列,列也必需拥有相同的数据类型. union all select 则取的是两 ...
- CRM 权限与分派不一样问题
问题描述: 1 userA用户 为 区域经理; 2 区域经理(角色) 为 分派给userA的安全角色; 3 区域经理(角色) 设置了对实体 客户 的读权限为 业务部门级; 按正常来说userA 应 ...
- NBU AIX ORACLE10G RAC恢复到AIX单实例(表空间恢复)
ln -s /usr/openv/netbackup/bin/libobk.a64 /oraclev3/product/10.2.0/lib/libobk.a不建软连接会报如下错误: using ta ...
- asm createdisk时提示没有权限
[root@linux Packages]# /etc/init.d/oracleasm createdisk asm1 /dev/sdg1Marking disk "asm1" ...