HDU 5720 Wool
来源:互联网 发布:java package类 编辑:程序博客网 时间:2024/04/29 05:41
题目链接:HDU5720
Wool
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 829 Accepted Submission(s): 239
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 aren 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.
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
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
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 integern,L,R (2≤n≤105,1≤L≤R≤1018) .
The second line containsn integers, the i -th integer denotes ai (1≤ai≤1018) .
For each test case, the first line of input contains single integer
The second line contains
Output
For each test case, print the number of ways to throw a stick.
Sample Input
22 1 31 14 3 101 1 2 4
Sample Output
25HintIn the first example, $ 2, 3 $ are available.In the second example, $ 6, 7, 8, 9, 10 $ are available.
题目分析:首先排序是必要的,从小到大排序。由于若a,b已经固定(a>=b),c<=a-b||c>=a+b才能保证不会构成三角形。对于排好序的数列a[0~n-1]中任意一个数a[i]而言,a[i+1]是可以构成最小的解空间来满足所有的情况。所以从左枚举a[i],并求出不可取的线段范围求并,其间统计答案,复杂度O(nlogn)。
比赛过程中是对所有可取空间求交集,结果一直在WA。最后5分钟才发现应该对不可取空间求并,改了7分钟在杭电上1A了。
//// main.cpp// Wool//// Created by teddywang on 2016/7/17.// Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;long long int a[100019];int main(){ long long int T,L,R,l,r; int n; cin>>T; while(T--) { memset(a,0,sizeof(a)); cin>>n>>L>>R; for(int i=0;i<n;i++) { scanf("%lld",&a[i]); } sort(a,a+n); long long int ans=0; for(int i=n-1;i>=1;i--) { l=a[i]-a[i-1]; r=a[i]+a[i-1]; if(l>R||r<L) continue; long long int buf=R-r+1; if(buf<0) buf=0; ans+=buf; R=min(l,R); if(R<L) break; } long long int buf=R-L+1; if(buf<0) buf=0; ans+=buf; cout<<ans<<endl; }}
0 0
- Hdu 5720 Wool
- hdu 5720 Wool
- HDU 5720 Wool
- HDU-5720 Wool
- hdu 5720 wool
- HDU 5720 Wool
- HDU 5720 Wool (贪心)
- HDU 5720 Wool(排序 遍历)
- 【HDU 5720】Wool(排序+排序/扫描线。。。)
- hdu 5720 Wool(贪心,扫描线,区间合并)
- HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)
- hdu 5720 - Wool (线段交的长度)
- HDOJ 5720 Wool
- codeforces 148 Not Wool Sequences
- codeforces 148 Not Wool Sequences
- China-Australia FTA Boosts Wool Industry
- CF 238A Not Wool Sequences
- 【HDOJ】【BestCoder 2nd Anniversary】1003 Wool
- JDBC基本操作
- Android Service 详解(包含AIDL)
- react-native init 项目后出现 unexpected token <
- Codeforces 444C DZY Loves Colors(线段树)
- 为什么使用对象指针而不是使用对象本身?
- HDU 5720 Wool
- C++修炼之路 map
- malloc动态申请内存
- UESTC1133 菲波拉契数制 01背包思想
- CSDN-MarkDown-模板
- libcurl中curl_easy_setopt()和curl_easy_perform()函数使用中文详解
- Android之图片缓存
- 利用ServeltConText制作留言板
- Android仿QQ通讯录分组展示ExpandableListView