UVA1616-Caravan Robbers(二分)
Accept: 96 Submit: 946
Time Limit: 3000 mSec
Problem Description
Long long ago in a far far away land there were two great cities and The Great Caravan Road between them. Many robber gangs “worked” on that road. By an old custom the i-th band robbed all merchants that dared to travel between ai and bi miles of The Great Caravan Road. The custom was old, but a clever one, as there were no two distinct i and j such that ai ≤ aj and bj ≤ bi. Still when intervals controlled by two gangs intersected, bloody fights erupted occasionally. Gang leaders decided to end those wars. They decided to assign each gang a new interval such that all new intervals do not intersect (to avoid bloodshed), for each gang their new interval is subinterval of the old one (to respect the old custom), and all new intervals are of equal length (to keep things fair). You are hired to compute the maximal possible length of an interval that each gang would control after redistribution.
Input
The input will contain several test cases, each of them as described below. The first line contains n (1 ≤ n ≤ 100000) — the number of gangs. Each of the next n lines contains information about one of the gangs — two integer numbers ai and bi (0 ≤ ai < bi ≤ 1000000). Data provided in the input file conforms to the conditions laid out in the problem statement.
Output
Note for the sample:
In the above example, one possible set of new intervals that each gang would control after redistribution is given below.
Sample Input
2 6
1 4
8 12
Sample Output
5/2
题解:最大化最小值,这个题二分答案的感觉是十分明显的,操作也很简单,就是精度要求比较高,关键一步在于最后的分数化小数,实在不会,参考了别人的代码,感觉很奇怪,主体操作能理解,就是枚举分母,计算分子,看该分数与答案的绝对误差,如果比当前解小,那就更新当前解,难以理解的地方在于分母枚举上限的选取,居然是线段的个数???(恳请大佬指教orz)
#include <bits/stdc++.h> using namespace std; const int maxn = + ;
const double eps = 1e-; int n; struct Inter {
int le, ri;
Inter(int le = , int ri = ) : le(le), ri(ri) {}
bool operator < (const Inter &a)const {
return le < a.le;
}
}inter[maxn]; bool Judge(double len) {
double pos = inter[].le + len;
if (pos > inter[].ri + eps) return false;
for (int i = ; i < n; i++) {
pos = pos > inter[i].le ? pos : inter[i].le;
pos += len;
if (pos > inter[i].ri + eps) return false;
}
return true;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) {
scanf("%d%d", &inter[i].le, &inter[i].ri);
} sort(inter, inter + n); double l = 0.0, r = 1000000.0;
double ans = 0.0;
while (l + eps < r) {
double mid = (l + r) / ;
if (Judge(mid)) {
ans = l = mid;
}
else r = mid;
} int rp = , rq = ;
for (int p, q = ; q <= n; q++) {
p = round(ans*q);
if (fabs(1.0*p / q - ans) < fabs(1.0*rp / rq - ans)) {
rp = p, rq = q;
}
} printf("%d/%d\n", rp, rq);
}
return ;
}
UVA1616-Caravan Robbers(二分)的更多相关文章
- UVa 1616 Caravan Robbers (二分+贪心)
题意:给定 n 个区间,然后把它们变成等长的,并且不相交,问最大长度. 析:首先是二分最大长度,这个地方精度卡的太厉害了,都卡到1e-9了,平时一般的1e-8就行,二分后判断是不是满足不相交,找出最长 ...
- UVA 1616 Caravan Robbers 商队抢劫者(二分)
x越大越难满足条件,二分,每次贪心的选区间判断是否合法.此题精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差. #include& ...
- UVa - 1616 - Caravan Robbers
二分找到最大长度,最后输出的时候转化成分数,比较有技巧性. AC代码: #include <iostream> #include <cstdio> #include <c ...
- 【习题 8-14 UVA - 1616】Caravan Robbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分长度. 显然长度越长.就越不可能. 二分的时候.可以不用管精度. 直接指定一个二分次数的上限就好. 判断长度是否可行.直接用贪心 ...
- NEERC2012
NEERC2012 A - Addictive Bubbles 题目描述:有一个\(n \times m\)的棋盘,还有不同颜色的棋子若干个,每次可以消去一个同种颜色的联通块,得到的分数为联通块中的棋 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- [bzoj2653][middle] (二分 + 主席树)
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
随机推荐
- 【Spring】27、JPA 实现乐观锁@Version注解的使用
持久层使用jpa时,默认提供了一个注解@Version来实现乐观锁 简单来说就是用一个version字段来充当乐观锁的作用.先来设计实体类 /** * Created by xujingfeng on ...
- Hibernate(十四)抓取策略
抓取策略: 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.Hibernate的抓取策略是Hibernate提升性能的一 ...
- Asp.Net中对操作Sql Server 简单处理的SqlDB类
好久不接触这些闲暇时间回顾一下以前的基础.因为平常使用的时候都是直接调用SqlDB.dll这个类.先看这个类的结构 纸上得来终觉浅,绝知此事要躬行.个人觉得里面的标准操作就是对数据库增删查改 .特别适 ...
- react-conponent-secondesElapsed
<!DOCTYPE html> <html> <head> <script src="../../build/react.js">& ...
- BUG -Failed to compile.
检查代码发现: 图片的路径写错了 改回正确路径页面可以正常显示
- [HTML/CSS]下拉菜单
原理:先让下拉菜单隐藏,鼠标移到的时候在显示出来 1>display 无动画效果,图片是秒出 2>opacity 有动画效果,我这里是1S出现,推荐配合绝对定位使用
- java程序存入数据库中文乱码解决方案
一.问题描述 背景:代码迁移,ssm框架在插入数据到mysql数据库时,中文乱码.代码中的编码配置没有问题,因为该项目代码以前使用过,没有问题.现在换了数据库,数据库配置也做了修改,统一使用utf8, ...
- Human Motion Analysis with Wearable Inertial Sensors——阅读3
Human Motion Analysis with Wearable Inertial Sensors——阅读3 四元数方向滤波器 之前的研究开发了一种自适应增益互补滤波器,并结合高斯 - 牛顿优化 ...
- Python入门基础之变量和数据类型
在Python中,能够直接处理的数据类型有以下几种: 一.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序中,整数的表示方法和数学上的写法一模一样,例如:1,100,-80 ...
- springboot 学习之路 5(打成war包部署tomcat)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...