hdu 5920 Wool 思路

Wool

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

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 (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).

Output

For each test case, print the number of ways to throw a stick.

Sample Input

2
2 1 3
1 1
4 3 10
1 1 2 4

Sample Output

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[N];
struct is
{
    ll x,y;
}gg[N];
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;
}
时间: 2024-08-09 15:05:30

hdu 5920 Wool 思路的相关文章

D - Ugly Problem HDU - 5920

D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must represent that number by sum of palindromic numbers. A palindromic number is a positive integer such that if you write out that integer as a string i

HDU - 5920 Ugly Problem 求解第一个小于n的回文数

http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半部分复制过去,如果太大,那么早到第一个会使得其太大的点,减1,然后对应的中间的变成9 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <a

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

hdu 5701(区间查询思路题)

中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 999    Accepted Submission(s): 383 Problem Description 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数. 现在有n个数,每个数都是独一无二的,求出每个数在多少个包

HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)

Wool Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 686    Accepted Submission(s): 192 Problem Description At dawn, Venus sets a second task for Psyche. She is to cross a river and fetch gol

HDU 1024 解题思路

题目: 给出S1,S2....Sn,这一个序列,把这个序列分成m段,使这m段的和最大,求出这个最大值是多少 思路: 很明显的dp问题,画一个表格(代表二维数组dp),dp[i][j]表示 :前j个数字分成i段的序列和最大是多少. 对于任意的 i , j , dp[i][j] 的值无非取两种: 第一种:前j-1个数字分成i组 , 就已经最大了,那么就取dp[i][j-1](当然i<j,如果i=j就是第二种情况了),这是不取第j个数字的情况,表达式为:dp[ i ][ j ] = dp[ i ][

hdu 5181 numbers——思路+区间DP

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.html 原来卡特兰数的这个问题还能区间DP…… XO #include<cstdio> #include<cstring> #include<algorithm> #define ll long long using namespace std; int rdn() { in

HDU 平分宝石(思路题)

Problem H Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 48   Accepted Submission(s) : 16 Special Judge Problem Description DreamGrid has $n$ classmates numbered from $1$ to $n$. Some of them a

【高精度 JAVA】HDU 5920 Ugly Problem

题意 给你一个数字n(n < 10^1000),将其拆成若干个回文串(不超过50个)输出拆分方案 分析 不难想到,我们可以每次给n减一个小于他的最大的回文串,这样能够尽量构造出最少数量的回文串,方法可以使直接将前一半反转贴到后一半,如果比原来的数字大,那么前一半减少1,再反转贴为后一半 比较坑的地方就是 如果构造出来的是11比n大, 那么前一半-1变成了00 ,特判一下,应该为9:如果构造出来是101比n大,应该特判为99 这个题没太多好讲的,我想在这里记录一下java编程心得,以后也会更新 主