题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值。最小距离定义为:如果点在区间内,那么最小距离为0,否则为min(pos-L[i],R[i]-pos)。

解法:当然要排个序,仔细想想会发现我们要找的区间的位置满足二分性质,即如果此时pos-L[mid] >= R[mid]-pos,那么我们要找的区间肯定是mid或大于mid,否则,我们要找的区间一定是mid即mid以下。二分找到即可。预处理时要把嵌套在别的区间里的区间忽略掉,因为外面那个区间一定比他更优。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
#define N 100007
#define M 22 struct node
{
ll l,r;
}p[N],np[N];
ll L[N],R[N];
int tot;
ll pos; int cmp(node ka,node kb)
{
return ka.l < kb.l;
} ll get(int mid)
{
if(mid > tot || mid < )
return ;
if(L[mid] > pos || R[mid] < pos)
return ;
return min(pos-L[mid],R[mid]-pos);
} int main()
{
int t,cs = ,n,m,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
scanf("%lld%lld",&p[i].l,&p[i].r);
sort(p+,p+n+,cmp);
tot = ;
for(i=;i<=n;i++)
{
if(p[i].r > p[tot].r)
{
tot++;
p[tot] = p[i];
}
}
for(i=;i<=tot;i++)
{
L[i] = p[i].l;
R[i] = p[i].r;
}
printf("Case %d:\n",cs++);
while(m--)
{
int ans = ;
scanf("%lld",&pos);
int low,high;
low = ,high = tot;
while(low <= high)
{
int mid = (low+high)/;
if(R[mid]-pos <= pos-L[mid])
low = mid+;
else
high = mid-;
}
printf("%lld\n",max(get(low),get(high)));
}
}
return ;
}

UVALive 6656 Watching the Kangaroo --二分的更多相关文章

  1. 6656 Watching the Kangaroo

    6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...

  2. UVa 12715 Watching the Kangaroo(二分)

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

  3. Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  4. UVALive 3635 Pie 切糕大师 二分

    题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...

  5. UVALive 2949 Elevator Stopping Plan(二分 + 贪心)

    ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. ...

  6. UVALive 5903 Piece it together 二分匹配,拆点 难度:1

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. UVALive - 3211 Now or later (二分+2SAT)

    题目链接 题意:有n架飞机,每架飞机有两个着陆时间点可以选,要求任意两架飞机的着陆时间之差不超过k,求k的最大值. 解法:由于每架飞机都有两个选择,并且必选且只能选其中一个,时间冲突也是发生在两架飞机 ...

  8. UVALive - 7427 the math 【二分匹配】

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

随机推荐

  1. Docker on CentOS for beginners

    Introduction The article will introduce Docker on CentOS. Key concepts Docker Docker is the world's ...

  2. ASP.NET Web API 数据提供系统相关类型及其关系

  3. Dalvik VM和JVM 的比较

    dx:dx工具用于将.class字节码(bytecode)转换为Android字节码(保存在.dex文件中)这个字节码文件 是给 Android 的 Java 虚拟机(Dalvik虚拟机)运行用的. ...

  4. spring task定时器笔记

    定时器有两种方式 1.延迟启动 <bean id="timerTaskRunnerChain" class="bingo.uam.task.TimerTaskRun ...

  5. SharePoint 2013 调用WCF服务简单示例

    内容比较简单,主要记录自己使用SharePoint 2013WCF服务遇到的小问题和小经验,分享给大家,希望能够给需要的人有所帮助.好吧,进入正题! 第一部分 SharePoint 2013调用自带W ...

  6. Microsoft Dynamics CRM 2013 --选项集的多选

    由于从Microsoft Dynamics CRM 2011到Microsoft Dynamics CRM 2013,界面的风格发生了很大的变化 故原先在2011上开发的选项集多选在2013上面已经不 ...

  7. 刚刚结束了公司EP流程,开始KMS项目开发了

    刚刚结束了公司EP流程,开始KMS项目开发了 EP流程:AGpoint+MOSS+C# KMS:MOSS上的文档管理系统

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q97-Q99)

    04 }Which code segment should you add at line 03?A. currentItem["ClassificationMetadata"] ...

  9. Fresco 使用笔记(一):加载gif图片并播放

    项目总结 --------------------------------------------------------------------- 前言: 项目中图文混合使用的太多太多了,但是绝大部 ...

  10. Mac OS X 10.10 Yosemite下面解决XAMPP无法开启mysql的问题

    首次使用XAMPP但是却遇到一个MySQL无法启动的缘故,不知道怎么解决,经过百度 找到解决方案: 在xampp安装目录下找到xamp这个文件(默认路径是:/Applications/XAMPP/xa ...