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)

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = + ;
  6. const double eps = 1e-;
  7.  
  8. int n;
  9.  
  10. struct Inter {
  11. int le, ri;
  12. Inter(int le = , int ri = ) : le(le), ri(ri) {}
  13. bool operator < (const Inter &a)const {
  14. return le < a.le;
  15. }
  16. }inter[maxn];
  17.  
  18. bool Judge(double len) {
  19. double pos = inter[].le + len;
  20. if (pos > inter[].ri + eps) return false;
  21. for (int i = ; i < n; i++) {
  22. pos = pos > inter[i].le ? pos : inter[i].le;
  23. pos += len;
  24. if (pos > inter[i].ri + eps) return false;
  25. }
  26. return true;
  27. }
  28.  
  29. int main()
  30. {
  31. //freopen("input.txt", "r", stdin);
  32. while (~scanf("%d", &n)) {
  33. for (int i = ; i < n; i++) {
  34. scanf("%d%d", &inter[i].le, &inter[i].ri);
  35. }
  36.  
  37. sort(inter, inter + n);
  38.  
  39. double l = 0.0, r = 1000000.0;
  40. double ans = 0.0;
  41. while (l + eps < r) {
  42. double mid = (l + r) / ;
  43. if (Judge(mid)) {
  44. ans = l = mid;
  45. }
  46. else r = mid;
  47. }
  48.  
  49. int rp = , rq = ;
  50. for (int p, q = ; q <= n; q++) {
  51. p = round(ans*q);
  52. if (fabs(1.0*p / q - ans) < fabs(1.0*rp / rq - ans)) {
  53. rp = p, rq = q;
  54. }
  55. }
  56.  
  57. printf("%d/%d\n", rp, rq);
  58. }
  59. return ;
  60. }

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. 【Java每日一题】20170119

    20170118问题解析请点击今日问题下方的“[Java每日一题]20170119”查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; import jav ...

  2. spring boot之hello

    自己使用springboot也已经写过一段时间的代码,但是对springboot真正运行的流程还是有点模糊,今天写出自己对springboot的认识,如有不对,还请各位大佬不吝赐教,话不多说,直接上代 ...

  3. Swap file ".BranchModel.class.php.swp" already exists!

    在 Linux 下的 vim 编辑过程中,由于某种原因异常退出正在编辑的文件,再次编辑该文件时,会出现如下提示: 1.解决方法:直接 ll -a -a 列出目录下的所有文件,包括以 . 开头的隐含文件 ...

  4. 人类阅读的优越方式打印php数组

    在程序开发过程中:打印数据进行查看调试是非常频繁的:如果没有一种易于阅读的样式那是相当痛苦的:先定义一个数组: $array=array( 't0'=>'test0', 't1'=>'te ...

  5. angular 分页插件的使用

    html: <pagination total-items="totalItems" ng-model="currentPage" items-per-p ...

  6. 2018-11-27 中文代码示例之Programming in Scala笔记第七八章

    续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...

  7. 运行 python *.py 文件出错,如:python a.py

    运行 python *.py 文件出错,如:python a.py(下图) 原因:没有安装web.py 解决:下载并安装 网址:http://webpy.org/install#install  (h ...

  8. Android IPC机制(二)用Messenger进行进程间通信

    Messenger可以在不同进程中传递Message对象,我们在Message中加入我们想要传的数据就可以在进程间的进行数据传递了.Messenger是一种轻量级的IPC方案并对AIDL 进行了封装, ...

  9. video 标签在微信浏览器的问题解决方法

    最近做的些web页面,内嵌许多小视频,在ios和安卓手机上播放时,遇到不少问题: 在微信浏览器内播放时,视频会自动全屏 如果每个视频都有一张自定义的图片作为封面,在显示视频的同时,如果没有给这个视频设 ...

  10. 设计模式—桥接模式的C++实现

    这是Bwar在2009年写的设计模式C++实现,代码均可编译可运行,一直存在自己的电脑里,曾经在团队技术分享中分享过,现搬到线上来. 1. 装饰模式简述 1.1 目的 将抽象部分与它的实现部分分离,使 ...