6656 Watching the Kangaroo

本文介绍了一种用于监测袋鼠位置并选择最佳显示屏显示袋鼠的算法。该算法通过计算不同摄像头覆盖范围内的袋鼠位置,确定哪个显示屏能够提供最佳观看体验。文章详细解释了算法的工作原理,并提供了一个具体的例子来说明如何计算每个显示屏的覆盖范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6656 Watching the Kangaroo

Day by day number of Kangaroos is decreasing justlike tiger, whale or lions. So I decided to make them a sanctuary where theywill live peacefully. I do not let visitors go near them. So I planted somedisplay screen outside the sanctuary. For this problem, you may assume thesanctuary to be a long line of 1000000000 unit distance. The leftmost positionis marked with 0 and the rightmost position with 1000000000. There are at most100000 cameras on this line. Each of the cameras is described with ( L , R )which means that camera covers a range from position L to position R inclusive.Each of the cameras has their associated display screens outside where the visitorscan see the Kangaroos.

 

Now for the convenience of the spectators weannounce time to time: “Kangaroo appears in Screen 3", “Kangaroo appearsin Screen 7" and so on. I have some employees who continuously monitor theposition of Kangaroos and inform the system (here position is a marker). Thesystem chooses the best screen to display that animal based on the coverage.The coverage of a screen for a position x is defined as follows:

 

If the position x is outside the range of thatscreen (i.e. x < L or x > R) then the coverage is zero. Otherwise thecoverage is min(x-L, R-x).

 

An example will make it clear:

Suppose there are four screens covering the range(7, 15), (14, 100), (8, 10) and (1, 11). Now say one Kangaroo appears at x =10.

First screen has coverage of 3 unit around x =10. Because x = 10 is within (7, 15) and min(10-7; 15-10) = min(3,5) = 3.

Second screen has coverage of 0 unit around x =10. Because x = 10 does not belong to the range (14, 100).

Third screen has coverage of 0 unit around x =10. Because though x = 10 is within (8, 10) but min(10-8,10-10) = 0.

Fourth screen has coverage of 1 unit around x =10. Because x = 10 is within (1, 11) and min(10-1,11-10) = 1.

So which one is better? Obviously the first one,as it has the maximum coverage.

 

So you are given the ranges of the screens andthe positions the kangaroo appears. For each position of the Kangaroo you areto tell me the maximum coverage you can have with any of the screens.

 

 

Input

First line of the test file contains T (T <=3), number of test cases. Hence T cases follow. For each case you are given N,M in the first line; N is the number of screens and M is the number of Kangarooappearance (1<=N, M<=100000). In the next N lines you are given the rangeof screens L R (0<=L<=R<=1000000000). Next M lines contain theposition of the Kangaroo | an integer x (0<=x<=1000000000).


#include <iostream>
#include <algorithm>
#include <cstdio>
#define MAXN 100010
#define INF 0x7fffffff
using namespace std;
typedef struct point
{
    int l,r;
} point;
point p[110000];
int n,x;
int fun(int s)
{
    if (s<0||s>=n) return 0;
    if (x<p[s].l||p[s].r<x) return 0;
    return min(x-p[s].l,p[s].r-x);
}
bool cmp(point x,point y)
{
    if(x.l==y.l)return x.r>y.r;
    return x.l<y.l;
}
int main()
{
    int T,i,m,j,nu,l,r,mid;
    scanf("%d",&T);
    for(i=1; i<=T; i++)
    {
        scanf("%d%d",&n,&m);
        for(j=0; j<n; j++)
            scanf("%d%d",&p[j].l,&p[j].r);
        sort(p,p+n,cmp);
        nu=1;
        for(j=1; j<n; j++)      //利用sort if(x.l==y.l)return x.r>y.r;优化,把p[nu].l<p[j].l&&p[j].r<q[nu].r的去掉
        {
            if(p[j].r>p[nu-1].r)
                p[nu++]=p[j];
        }
        n=nu;
        printf("Case %d:\n",i);
        while(m--)
        {
            l=0,r=n-1;
            scanf("%d",&x);
            while(l<=r)             //二分;
            {
                mid=(l+r)>>1;
                if(x>p[mid].r||(x<=p[mid].r&&x>=p[mid].l&&x-p[mid].l>=p[mid].r-x))     //在(p[mid].l+p[mid].r)/2的右边;
                    l=mid+1;
                else r=mid-1;
            }
            printf("%d\n",max(fun(l),fun(r))); //画个图就知道结果有几种可能性,要一一考虑;
        }
    }
	return 0;
}

代码借鉴他人..........

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值