UVALive-6656-Watching the Kangaroo(二分)

來源:互聯網
上載者:User

標籤:des   style   io   os   ar   for   sp   cti   代碼   

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

Output
For each case print the case number. Hence print M lines, i-th containing the maximum coverage you
can have around i-th Kangaroo.
Warning: The judge input le is around 6 MB. So use faster I/O functions.


Sample Input
1
3 2
7 15
14 100
1 11
10
120
Sample Output
Case 1:
3
0


思路:把區間從中點分開分成兩個區間,然後分別查詢左邊和右邊的最大值。詳見代碼。


#include <cstdio>#include <algorithm>using namespace std;struct S{int l,r;}ls[100000],rs[100000];int idx1[100000],idx2[100000];bool cmp1(const struct S &a,const struct S &b){    if(a.l==b.l) return a.r>b.r;    return a.l<b.l;}bool cmp2(const struct S &a,const struct S &b){    if(a.r==b.r) return a.l>b.l;    return a.r<b.r;}int main(){    int T,n,m,i,a,b,s,e,mid,cnt1,cnt2,temp,ans,cases=1;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(i=0;i<n;i++)        {            scanf("%d%d",&a,&b);            mid=(a+b)>>1;                        ls[i].l=a;//左邊            ls[i].r=mid;                        rs[i].l=mid;//右邊            rs[i].r=b;            if((a+b)%2) rs[i].l++;//注意        }        sort(ls,ls+n,cmp1);        sort(rs,rs+n,cmp2);        cnt1=0;        temp=-1;        for(i=0;i<n;i++)//把無用的元素去掉        {            if(ls[i].r>temp)            {                idx1[cnt1++]=i;                temp=ls[i].r;            }        }        cnt2=0;        temp=1000000007;        for(i=n-1;i>=0;i--)//把無用的元素去掉        {            if(rs[i].l<temp)            {                idx2[cnt2++]=i;                temp=rs[i].l;            }        }        printf("Case %d:\n",cases++);        while(m--)        {            scanf("%d",&a);            if(a>=rs[idx2[0]].r || a<=ls[idx1[0]].l)//如果沒有被包含到任何區間            {                printf("0\n");                continue;            }            ans=0;            s=0,e=cnt1-1;            while(s<=e)//找左邊最大值            {                mid=(s+e)>>1;                if(ls[idx1[mid]].r>=a)                {                    ans=max(ans,a-ls[idx1[mid]].l);                    e=mid-1;                }                else s=mid+1;            }            s=0,e=cnt2-1;            while(s<=e)//找右邊最大值            {                mid=(s+e)>>1;                if(rs[idx2[mid]].l<=a)                {                    ans=max(ans,rs[idx2[mid]].r-a);                    e=mid-1;                }                else s=mid+1;            }            printf("%d\n",ans);        }    }}


UVALive-6656-Watching the Kangaroo(二分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.