HDU 5720 Wool

来源:互联网 发布:域名后缀cc 编辑:程序博客网 时间:2024/04/30 18:51
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 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.
 

Input
The first line of input contains an integer T (1T10), which denotes the number of test cases.

For each test case, the first line of input contains single integer n,L,R (2n105,1LR1018).

The second line contains n integers, the i-th integer denotes ai (1ai1018).
 

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
25
Hint
In the first example, $ 2, 3 $ are available.In the second example, $ 6, 7, 8, 9, 10 $ are available.

先排个序,找出相邻的两个长度的不可行区间,然后合并即可

#include<set>#include<map>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int N = 1e5 + 10;const int mod = 998244353;const int INF = 0x7FFFFFFF;int T, n, t;LL a[N], L, R, l[N], r[N], b[N], ans;bool check(LL &l, LL &r){l = max(l, L);  r = min(r, R);return l <= r;}bool cmp(const int &x, const int &y){return l[x] < l[y];}int main(){scanf("%d", &T);while (T--){scanf("%d%lld%lld", &n, &L, &R);rep(i, 1, n) scanf("%lld", &a[i]);sort(a + 1, a + n + 1);ans = t = 0;rep(i, 1, n - 1){l[i] = a[i + 1] - a[i] + 1;r[i] = a[i] + a[i + 1] - 1;if (check(l[i], r[i])) b[t++] = i;}sort(b, b + t, cmp);for (LL i = 0, j, k; i < t; i = j){for (j = i, k = r[b[i]]; j < t&&l[b[j]] <= k; j++) k = max(k, r[b[j]]);ans += k - l[b[i]] + 1;}printf("%lld\n", R - L + 1 - ans);}}


0 0
原创粉丝点击