Problem 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

For each test case, write to the output on a line by itself. Output the maximal possible length of an interval in miles as an irreducible fraction p/q.
Note for the sample:
In the above example, one possible set of new intervals that each gang would control after redistribution is given below.
• The first gang would control an interval between 7/2 = 3.5 and 12/2 = 6 miles which has length of 5/2 and is a subinterval of its original (2, 6).
• The second gang would control an interval between 2/2 = 1 and 7/2 = 3.5 miles which has length of 5/2 and is a subinterval of its original (1, 4).
• The third gang would control an interval between 16/2 = 8 and 21/2 = 10.5 miles which has length of 5/2 and is a subinterval of its original (8, 12).
 

 Sample Input

3
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(二分)的更多相关文章

  1. UVa 1616 Caravan Robbers (二分+贪心)

    题意:给定 n 个区间,然后把它们变成等长的,并且不相交,问最大长度. 析:首先是二分最大长度,这个地方精度卡的太厉害了,都卡到1e-9了,平时一般的1e-8就行,二分后判断是不是满足不相交,找出最长 ...

  2. UVA 1616 Caravan Robbers 商队抢劫者(二分)

    x越大越难满足条件,二分,每次贪心的选区间判断是否合法.此题精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差. #include& ...

  3. UVa - 1616 - Caravan Robbers

    二分找到最大长度,最后输出的时候转化成分数,比较有技巧性. AC代码: #include <iostream> #include <cstdio> #include <c ...

  4. 【习题 8-14 UVA - 1616】Caravan Robbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分长度. 显然长度越长.就越不可能. 二分的时候.可以不用管精度. 直接指定一个二分次数的上限就好. 判断长度是否可行.直接用贪心 ...

  5. NEERC2012

    NEERC2012 A - Addictive Bubbles 题目描述:有一个\(n \times m\)的棋盘,还有不同颜色的棋子若干个,每次可以消去一个同种颜色的联通块,得到的分数为联通块中的棋 ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  8. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  9. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

随机推荐

  1. mybatis全局属性(全局变量)

    mybatis全局属性(全局变量):方法1:在 properties 元素体内,使用<property>标签定义的属性方法2:在 properties 元素中, 使用 resource 或 ...

  2. linux (1): 启动

    很早就接触过linux,但是都是一知半解的用,连皮毛都算不上只记得几个命令而已,故决定好好学习一下linux,当前大环境下如果对linux不熟悉或者说不会基本的使用的话,会遇到很多问题. 历史: (大 ...

  3. cf1132E. Knapsack(搜索)

    题意 题目链接 Sol 看了status里面最短的代码..感觉自己真是菜的一批..直接爆搜居然可以过?..但是现在还没终测所以可能会fst.. #include<bits/stdc++.h> ...

  4. RabbitMQ 生产消息并放入队列

    前提已有 Exchange, Queue, Routing Key, 可以在 web 页面点击鼠标创建, 也可在消费端通过代码自动创建 web 页面配置步骤: https://www.cnblogs. ...

  5. DEM山体阴影原理以及算法具体解释

    山体阴影原理以及算法具体解释 山体阴影基本原理: 山体阴影是假想一个光源在某个方向和某个太阳高度的模拟下.用过临近像元的计算来生成一副0-255的灰度图. 一.山体阴影的主要參数: 1.  太阳光线的 ...

  6. loadrunner 脚本优化-参数化之场景中的参数化取值

    脚本优化-场景中的参数化取值 by:授客 QQ:1033553122   Action() { lr_eval_string("{NewParam}"); lr_eval_stri ...

  7. (后台)SQL Server 代理(已禁用代理 XP) 怎么解决(转)

    百度知道搜索的答案: 在SQL Server Management Studio中连接到SQL Server实例后,会显示“SQL Server 代理”节点.如果当前该实例的Agent服务没有启动,“ ...

  8. 跨域调用接口的方法之一:$.ajaxSetup()

    跨域查询接口的数据,之前在公司时有发生过,产生的原因是,本地请求的域名或IP地址不一致,解除方法,也是修改域名和IP地址.比如: 接口中的数据来自IP地址:192.168.1.23/get.php 如 ...

  9. Fiddler做代理服务器时添加X-Forwarder-For转发真实客户端ip

    修改CustomRules.js 菜单: Rules->Customize Rules (ctrl+R) 在 static function OnBeforeRequest(oSession: ...

  10. Scala隐式参数

    Scala方法可以具有隐式参数列表,由参数列表开头的implicit关键字标记.如果参数列表中的参数没有像往常一样传递,Scala将查看它是否可以获得正确类型的隐式值,如果可以,将自动传递. Scal ...