hdu 5920 Wool 思路
2016-07-17 22:44
176 查看
Wool
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, print the number of ways to throw a stick.
[align=left]Sample Input[/align]
2
2 1 3
1 1
4 3 10
1 1 2 4
[align=left]Sample Output[/align]
2
5
Hint
In the first example, $ 2, 3 $ are available.
In the second example, $ 6, 7, 8, 9, 10 $ are available.
思路:求出每条边的合法区间,区间合并一下,根据L,R,求ans;
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=1e5+10,M=1e7+10,inf=1e9+10; const ll mod=998244353; ll a ; struct is { ll x,y; }gg ; int cmp(is x,is y) { if(x.y!=y.y) return x.y<y.y; return x.x<y.x; } ll check(ll x,ll y,ll l,ll r) { ll maxx=max(x,l); ll minn=min(r,y); if(maxx<=minn) return minn-maxx+1; return 0; } is he(ll x,ll y,ll l,ll r) { is ans; ans.x=min(x,l); ans.y=max(r,y); return ans; } int main() { ll x,y,z,i,t; int T; ll L,R; scanf("%d",&T); while(T--) { scanf("%I64d%I64d%I64d",&x,&L,&R); for(i=1;i<=x;i++) scanf("%I64d",&a[i]); sort(a+1,a+x+1); for(i=1;i<x;i++) { gg[i].x=a[i+1]-a[i]+1; gg[i].y=a[i+1]+a[i]-1; } sort(gg+1,gg+x,cmp); int ji=0; gg[ji].x=gg[1].x; gg[ji].y=gg[1].y; ji++; for(i=2;i<x;i++) { if(gg[i].x<=gg[ji-1].y) { gg[ji-1]=he(gg[i].x,gg[i].y,gg[ji-1].x,gg[ji-1].y); } else { gg[ji].x=gg[i].x; gg[ji].y=gg[i].y; ji++; } } ll ans=0; for(i=0;i<ji;i++) { ans+=check(gg[i].x,gg[i].y,L,R); } printf("%I64d\n",R-L+1-ans); } return 0; }
相关文章推荐
- nginx把两个不同的网站挂到同一台服务器的80端口上
- 数据结构二叉树的实现,前序、中序、后序遍历
- POJ 3461 Oulipo [KMP模板题]
- JAVA继承和多态
- hdu 1588 Gauss Fibonacci (二分+矩阵快速幂)
- hdu 5719 Arrange 贪心
- 判断一个char*是不是utf8编码
- GitHub的初级使用方法
- 源码推荐(0717):基于AVPlayer的封装,按钮切换
- python-05:怎么开始写爬虫
- Git使用入门
- linux内核启动地址的确定
- linux内核启动地址的确定
- linux内核启动地址的确定
- linux内核启动地址的确定
- linux内核启动地址的确定
- linux内核启动地址的确定
- 设计模式---结构类型---代理
- 有一百盏灯,全部亮着并且从一到一百进行编号,对每盏灯做如下处理,如果该灯的编号能被1整除则拨一下开关,能被二整除再拨一下,以此类推,直到该灯的编号(不含编号本身)为至,问最后哪几盏灯亮着。
- HYSBZ 2243 染色(树链剖分 + 线段树区间合并)