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), ...
随机推荐
- Succession
Description The king in Utopia has died without an heir. Now several nobles in the country claim the ...
- 山东省赛-博弈-Game
id=1582" target="_blank" style="font-size:18px">点击打开题目链接 非常明显的一道博弈题目,可 ...
- c# 获取北京时间更新本地计算机
class UpdateDateTime { [DllImport("Kernel32.dll")] private static extern void SetLocalTime ...
- Systemd 基础(转)
Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置. 原文链接:http://www.ruanyifeng.com/blog/2016/03/systemd-tu ...
- Java中初级数值类型的大小, volatile和包装类wrapped type的比较
Java中的初级数值类型 Java是静态类型语言, 所有的变量必须先声明再使用. 其初级类型一共8种: boolean: 数据只包含1bit信息, 但是占空间为8-bit, 默认值为false byt ...
- iOS- 非整星的评分控件(支持小数)
概述 订单评论里实现星级评分控件: 简单整星评价与非整星的精评价. 详细 代码下载:http://www.demodashi.com/demo/10711.html 现在很多应用都有评分功能. 有了订 ...
- 【微信小程序】详解wx:if elif else的用法(搭配view、block)
1.搭配view <view wx:if="{{boolean==true}}"> <view class="bg_black">< ...
- PHP-深入学习Smarty
本文中的边界标签分别为"<{"和"}>" start-12, 都是静态模板中的内容, 即使函数也只是模板中的标签或者变量调解器; 13-end, 都 ...
- HDUOJ---2642Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submi ...
- iOS设计模式:简单工厂模式
1.简述 首先需要说明一下,简单工厂模式不属于23种GOF设计模式之一.它也称作静态工作方法模式,是工厂方法模式的特殊实现(也就是说工厂模式包含简单工厂模式).这里对简单工厂模式进行介绍,是为后面的工 ...