UVALive 6656 Watching the Kangaroo --二分
题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值。最小距离定义为:如果点在区间内,那么最小距离为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 --二分的更多相关文章
- 6656 Watching the Kangaroo
6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...
- UVa 12715 Watching the Kangaroo(二分)
题意:n条线段(n <= 100000) (L<=R <= 1e9) ,m组询问(m <= 100000) 每次询问一个点的覆盖范围的最大值.一个点x对于一条包括其的线段,覆盖 ...
- 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 ...
- UVALive 3635 Pie 切糕大师 二分
题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...
- UVALive 2949 Elevator Stopping Plan(二分 + 贪心)
ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. ...
- UVALive 5903 Piece it together 二分匹配,拆点 难度:1
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive - 3211 Now or later (二分+2SAT)
题目链接 题意:有n架飞机,每架飞机有两个着陆时间点可以选,要求任意两架飞机的着陆时间之差不超过k,求k的最大值. 解法:由于每架飞机都有两个选择,并且必选且只能选其中一个,时间冲突也是发生在两架飞机 ...
- UVALive - 7427 the math 【二分匹配】
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVaLive 3971 Assemble (水题二分+贪心)
题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...
随机推荐
- vmware mysql报kernel: EXT4-fs (dm-0): Unaligned AIO/DIO on inode 1055943 by mysqld; performance will be poor
kernel: EXT4-fs (dm-0): Unaligned AIO/DIO on inode 1055943 by mysqld; performance will be poor
- mysql root用户kill connection报ERROR 1095 (HY000): You are not owner of thread N
今日某系统mysql root用户kill connection时报ERROR 1095 (HY000): You are not owner of thread N 按说通过root用户具有supe ...
- db2死锁分析与处理
在数据库中,锁的主要功能是为了控制并发数据的完整性而引入的机制,在并发应用中出现锁现象并不可怕,锁现象通常分为死锁和锁等待两种情形. 死锁是因为两个并发的进程或者线程同时各自占有一个资源,又需要占有对 ...
- Oracle执行计划与统计信息的一些总结
[日期:2011-08-05]来源:Linux社区 作者:wangshengfeng1986211[字体:大 中 小] 2010-07-01 15:03 1.SET AUTOTRACE ON EXP ...
- Git使用ssh key
生成ssh key步骤 这里以配置github的ssh key为例: 1. 配置git用户名和邮箱 git config user.name "用户名" git config us ...
- 关于android4.3 Intel X86 Atom System Image的下载
今天建立android4.3模拟器的时候发现没有android4.3 Intel X86 Atom System Image可选,打开android SDK Manager 于是希望重现选择下载安装, ...
- C语言常量与指针
C语言功能强大而丰富,还表现在const与指针的结合使用上,对不同的问题,它提供不同的保护,特别有用的是指向常量的指针 本文地址:http://www.cnblogs.com/archimedes/p ...
- iOS设计模式之策略模式
策略模式(Strategy) 基本理解 面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类. 策略模式:它定义了算法家族,分别封装起来, ...
- 异步post请求之代理方法
#import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...
- IOS 简单动画 首尾式动画
首尾式动画 首尾式动画即通过实现控件由初始状态到结束状态的过程.(主要表现在控件的Frame 透明度 ) // // ViewController.m // CX 简单动画 // // Created ...