【HDU 5720】Wool(排序+排序/扫描线。。。)

【HDU 5720】Wool(排序+排序/扫描线。。。)


Wool

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 136    Accepted Submission(s): 41


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

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5722  5721  5719  5718  5717 

题目大意:

首先给出n,L,R,表示地上现在有n根树枝,你手里现有[L,R]长度的树枝各一根

要求选择一根树枝,不能与地上任意两根树枝构成三角形,问有多少种选法。


已知选出的树枝长度x满足对于地上任意两根树枝a,b,x <= abs(a-b) || x >= a+b,也就是x∉( abs(a-b) , a+b ),这样abs(a-b)与a+b就构成了一段不合法区间

要满足所有情况,只要让a与b尽量接近,这样abs(a-b)就保证尽量小,同时也保证了对于a为上界时,a+b尽量大。也就是说保证了在a选定时,上面的区间覆盖范围最广。

这样只要把地上树枝按从小到大排序,就可以构成n-1个不可行区间。[L,R]区间去掉这些不可行区间的并,就是最终答案。

这样从头往后遍历一遍即可


代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<LL,LL>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

LL x[111111];
Pr pr[111111];

int main()
{
	//fread("");
	//fwrite("");

	int t;
	scanf("%d",&t);

	while(t--)
	{
	
		int n;
		LL L,R;
	
		scanf("%d%lld%lld",&n,&L,&R);
	
		for(int i = 0; i < n; ++i) scanf("%lld",&x[i]);
	
		sort(x,x+n);
	
		for(int i = 0; i < n-1; ++i)
		{
			pr[i].first = x[i+1]-x[i];
			pr[i].second = x[i+1]+x[i];
		}
	
		sort(pr,pr+n-1);
		LL ans = 0;
		LL mx = L;
	
		for(int i = 0; i < n-1 && pr[i].first <= R; ++i)
		{
			if(pr[i].first >= mx) ans += (pr[i].first-mx+1);
			mx = max(mx,pr[i].second);
		}
		ans += max(0LL,R+1-mx);
		printf("%lld\n",ans);
	}

	return 0;
}



比赛的时候想麻烦了,往扫描线上想了= =不过还好思路没太大问题,错了几发,最后也对了

扫描线其实压缩出来就是上面的思路,区间那里思路是一样的,排序后找出n-1个区间,扫描线是记录了每个区间左右端点,左进右出,左+1,右-1

当计数器为0时,说明扫到当前未知出现空挡,也就是未被覆盖的区域,统计一下这段区域长度即可。


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cmath>
#define LL long long
#define Pr pair<int,int>
#define VI vector<int>

using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int msz = 10000;

LL x[111111];
struct Node
{
    LL x;
    int ad;
    bool operator <(const struct Node a)const
    {
        return x == a.x? ad > a.ad: x < a.x;
    }
};

Node nd[233333];

int main()
{

    int t,n;
    LL L,R;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%I64d%I64d",&n,&L,&R);

        for(int i = 0; i < n; ++i) scanf("%I64d",&x[i]);

        sort(x,x+n);

        int tp = 0;
        for(int i = 1; i < n; ++i)
        {
            nd[tp].x = x[i]-x[i-1]+1;
            nd[tp++].ad = 1;
            nd[tp].x = x[i]+x[i-1];
            nd[tp++].ad = -1;
        }

        int cnt = 0;
        LL pre = 1;
        LL ans = 0;
        sort(nd,nd+tp);

        for(int i = 0; i < tp; ++i)
        {
            if(cnt == 0)
            {
                ans += max(0LL,min(R+1,nd[i].x)-max(pre,L));
            }
            cnt += nd[i].ad;
            pre = nd[i].x;
        }

        ans += max(0LL,R+1-max(pre,L));
        printf("%I64d\n",ans);
    }


    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值