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;
}
UVa 12715 Watching the Kangaroo(二分)的更多相关文章
- UVALive 6656 Watching the Kangaroo --二分
题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值.最小距离定义为:如果点在区间内,那么最小距离为0,否则为min(pos-L[i],R[i]-pos). 解法:当然要排个序, ...
- 6656 Watching the Kangaroo
6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- UVA 10668 - Expanding Rods(数学+二分)
UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...
- uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10539 (筛素数、二分查找) Almost Prime Numbers
题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- Uva 10339 - Watching Watches【数论,暴力】
题目链接:10339 - Watching Watches 题意:两个时钟,一个每天慢a秒,一个每天慢b秒,问两钟重新相遇的时刻 1圈有12 * 60 * 60秒,然后1圈 / abs(a - b), ...
随机推荐
- docker入门——构建镜像
前面我们已经介绍了如何拉取已经构建好的带有定制内容的Docker镜像,那么如何构建自己的镜像呢? 构建Docker镜像有以下两种方法: 使用docker commit命令. 使用docker buil ...
- linux下mysql-5.5.15安装详细步骤
linux下mysql-5.5.15安装详细步骤 注:该文档中用到的目录路径以及一些实际的值都是作为例子来用,具体的目录路径以各自安装时的环境为准 mysql运行时需要一个启动目录.一个安装目录和一个 ...
- Web Service 的工作原理(转载)
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- POSIX 线程 – pthread_sigmask
http://www.cnblogs.com/qq78292959/archive/2012/04/05/2432985.html 概念 按照 POSIX, 异步 (外部) 信号发送到整个进程. 所有 ...
- Android Gson解析json详解
目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...
- zookeeper工作原理解析
zookeeper一般用于distributed locking,并不适合用于distributed storage,由于zookeeper的每一个node.也叫做znode的存储容量限制是1M. z ...
- Drupal administration theme
Drupal允许为管理后台设置独立的theme,保存在系统变量variable_get('admin_theme'). Drupal使用全局变量$theme来保存当前请求对应的主题.Drupal在启动 ...
- 最新美行地图Z13升级攻略
原文地址:http://bbs.gpsuu.com/read.php?tid-231134.html 2013年11月16日订车,4S答应送导航,却没有提送什么导航.12月24日提车,DVD导航一体 ...
- Linux ssh服务开启秘钥和密码认证
问题描述: 实现Linux秘钥和密码同时认证 解决方案: vim /etc/ssh/sshd_config 基本参数: PermitRootLogin yes #允许root认证登录 Password ...
- .net完整的图文验证
摘自:http://blog.csdn.net/durongjian/article/details/4336380 一.创建ValidaeCode类库工程: 1.创建ValidaeCode类库工程, ...