UVALive 6656 Watching the Kangaroo (区间排序 + 二分)
来源:互联网 发布:斯凯奇淘宝旗舰店真假 编辑:程序博客网 时间:2024/05/16 17:43
大体题意:
给你最多10万个区间,和最多10万个查询, 每个查询会输入一个整数x,x在一个区间上有一个值,如果x不在区间内部,那么这个值为0,否则 为 到左端点和右端点的距离的最小值,要求求出每一个x 在所有区间值的最大值?
思路:
比赛时没有做出来,赛后补得,用二分做!比较巧妙!
先把每一个线段分成两半,左区间和右区间(注意::分区间一定要看好边界,WA了好几次= =)
然后给区间排序,
所有的左区间 按照 右端点由小到大排序,那么这样我们只需要找到第一个 右端点比x的大位置,那么下面的肯定也比x大了,然后我们想办法求出从这个位置 pos~n所有的区间最小的L,那么x-minL 就是最大值!!
右区间同样的道理处理!
在需要注意的就是二分了,手写二分不把握,可以多测试几组小样例试二分!
详细见代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000 + 10;const int inf = 1e9 + 10;int minl[maxn],maxr[maxn];struct Node{ int l,r; void read(int l_,int r_){ l = l_; r = r_; }}pl[maxn],pr[maxn];bool cmpl(const Node& lhs,const Node& rhs){ return lhs.r < rhs.r;}bool cmpr(const Node& lhs,const Node& rhs){ return lhs.l > rhs.l;}int main(){ int T,kase = 0; scanf("%d",&T); while(T--){ int n,m; scanf("%d %d",&n, &m); for (int i = 0; i < n; ++i){ int u,v; scanf("%d %d",&u, &v); int mid = u + v >> 1; if ((u + v) & 1){ pl[i].read(u,mid); pr[i].read(mid+1,v); }else { pl[i].read(u,mid); pr[i].read(mid,v); } } sort(pl,pl+n,cmpl); minl[n] = inf; for (int i = n-1; i >= 0; --i){ minl[i] = minl[i+1]; minl[i] = min(minl[i],pl[i].l); } sort(pr,pr+n,cmpr); maxr[n] = -inf; for (int i = n-1; i >= 0; --i){ maxr[i] = maxr[i+1]; maxr[i] = max(maxr[i],pr[i].r); } printf("Case %d:\n",++kase); for (int i = 0; i < m; ++i){ int x; scanf("%d",&x); int ans = 0; int l = 0,r = n-1; int pos = -1; while(l <= r){ int mid = l + r >> 1; if (pl[mid].r >= x){ r = mid-1; pos = mid; } else l = mid + 1; } if (pos != -1){ if (minl[pos] <= x) ans = max(ans,x-minl[pos]); } l = 0,r = n-1; pos = -1; while(l <= r){ int mid = l + r >> 1; if (pr[mid].l > x){ l = mid + 1; } else { r = mid - 1; pos = mid; } } if (pos != -1){ if (maxr[pos] >= x) ans = max(ans,-x+maxr[pos]); } printf("%d\n",ans); } } return 0;}
0 0
- UVALive 6656 Watching the Kangaroo (区间排序 + 二分)
- *UVALive 6656 - Watching the Kangaroo(二分)
- UVALive-6656-Watching the Kangaroo(二分)
- UVALive 6656 Watching the Kangaroo --二分
- UVALive 6656 Watching the Kangaroo(二分)
- 6656 Watching the Kangaroo
- UVALive 6656 Watching the Kangaroo 拆分线段,set维护
- UVa 12715 Watching the Kangaroo(二分)
- Watching the Kangaroo
- UVALive 6851 The Programmers(二分图多重匹配)
- UVALive 4683 Find The Number(容斥+二分)
- UVALive 5695 -The Last Puzzle -区间dp
- SOJ-2500(IP过滤,对区间排序,合并区间,然后对区间二分查找)
- UVALive 3938 Ray, Pass me the dishes (线段树区间合并)
- UVALive 6921Refraction(二分)
- uvalive 4254(二分+模拟)
- UVALive 3635 Pie (二分)
- UVALive - 3635 - Pie(二分)
- gem5跑HM的测试结果
- 速算
- Hibernate JPA注解 @Temporal(TemporalType.DATE) 日期注解!
- c++ pcsc
- PDF转换器怎么将PDF转换成Word格式的
- UVALive 6656 Watching the Kangaroo (区间排序 + 二分)
- JavaWEB查询数据库信息(JSTL)
- matlab SVM有监督学习
- ssh反连
- Linux系统信息
- Matlab中画图的说明
- Android Asynchronous Http Client
- 华为oj之求int型正整数在内存中存储时1的个数
- 添加环境变量