UVa 12715 Watching the Kangaroo(二分)

题意:n条线段(n <= 100000) (L<=R <= 1e9) ,m组询问(m <= 100000) 每次询问一个点的覆盖范围的最大值,一个点x对于一条包含其的线段,覆盖范围为min(x-L,R-x)

思路:考虑将线段一份为二,对于左边的那部分,以右端点排序,然后 二分找到右端点恰好满足的那个点为id,那么接下来要做的就是就是在[id,n]这个范围内找到L最小的那个点,可以通过求前缀最大来得到。那么左边最大距离为  seg[id].L-preLMax[id],右边最大的求法也是类似。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 100000+10;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
struct seg{
    int L,R;
    seg(int L = 0,int R = 0):L(L),R(R){}
};
bool cmp1(seg a,seg b) {
    if(a.R != b.R)  return a.R < b.R;
    else return a.L < b.L;
}
bool cmp2(seg a,seg b) {
    if(a.L != b.L)  return a.L < b.L;
    else return a.R < b.R;
}
vector<seg>lft,rgt;

int preLMax[maxn],preRMax[maxn];
int n,m;

int main(){

    int ncase,T=1;
    cin >> ncase;
    while(ncase--) {
        lft.clear();
        rgt.clear();
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++) {
            int L,R;
            scanf("%d%d",&L,&R);
            int mid = (L+R)>>1;
            lft.push_back(seg(L,mid));
            rgt.push_back(seg(mid,R));
        }

        sort(lft.begin(),lft.end(),cmp1);
        preLMax[lft.size()] = 1e9;
        for(int i = lft.size()-1; i >= 0; i--) {
            preLMax[i] = min(preLMax[i+1],lft[i].L);
        }
        sort(rgt.begin(),rgt.end(),cmp2);
        preRMax[0] = rgt[0].R;
        for(int i = 1; i < rgt.size(); i++) {
            preRMax[i] = max(preRMax[i-1],rgt[i].R);
        }
        printf("Case %d:\n",T++);
        while(m--) {
            int x,ans=0;
            scanf("%d",&x);
            int L = 0,R = lft.size()-1;
            while(L <= R) {
                int mid = (L+R) >>1;
                if(lft[mid].R < x) {
                    L = mid+1;
                }else{
                    R = mid-1;
                }
            }
            ans = max(ans,x-preLMax[L]);
            L = 0,R = rgt.size()-1;
            while(L <= R) {
                int mid = (L+R) >>1;
                if(rgt[mid].L < x) {
                    L = mid+1;
                }else{
                    R = mid-1;
                }
            }
            ans = max(ans,preRMax[R]-x);
            ans = max(ans,0);
            printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-10-17 17:31:08

UVa 12715 Watching the Kangaroo(二分)的相关文章

UVA 10668 - Expanding Rods(数学+二分)

UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图可以很容易推出公式,设圆弧扇形部弧度r,那么可以计算出铁棒长度为lr/sin(r)这个公式在[0, pi/2]是单调递增的,所以可以用二分法去求解 要注意的一点是最后答案计算过程中带入mid,之前是带入x(二分的左边值),可实际上x是可能等于0的,而带入mid,由于是double型,所以mid实际上表示是一个非常趋近0

UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

先从中找出性能最好的那个数, 在用钱比较少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; map<string,int> vic;//以字符映射数字 int end,start; int num; int

uva 714 Copying Books (二分)

uva 714 Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its co

UVA 10487 Closest Sums(二分)

UVA 10487 Closest Sums Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple cases. Each

UVA - 10487 - Closest Sums (二分求解)

传送:UVA - 10487 10487 Closest Sums Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple c

uva 1335 Beijing Guards(二分)

uva 1335 Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way

UVA 1471 - Defense Lines(扫描+二分)

UVA 1471 - Defense Lines 题目链接 题意:给定一个序列,要求删去一个连续子序列后,得到的序列有一个最长的连续递增序列,输出最长连续递增序列长度 思路:先左右扫描一遍,把每个位置往左和往右的最大长度记录下来,然后在从左往右扫描一遍,开一个数组Min用来记录长度i的序列,最后一位的最小值,这个序列是满足单调性的,因为递增序列肯定是1,2,3,4...这样不断往上加的,如果遇到一个a[i]比较小的,就维护Min相应长度下的值,这样在这个单调递增的序列中,每次就可以二分找出最后一

UVA - 11277 Cyclic Polygons(二分)

题意:已知圆的内接多边形的各个边长,求多边形的面积. 分析: 1.因为是圆的内接多边形,将多边形的每个顶点与圆心相连,多边形的面积就等于被分隔成的各三角形之和. 2.根据海伦公式,任意一个三角形的面积为:double p = (2 * r + a[i]) / 2,S = sqrt(p * (p - r) * (p - r) * (p - a[i])),a[i]为多边形某条边的长度,由此可以表示出多边形的面积. 3.对于任意一个三角形,设其为半径的两条边的夹角为α,则sin(α/2) = (a[i

【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents hadto be re-written by hand by so called scribers. The scriber had been given a book and after severalmonths he finished its copy. One of the most famo