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 = 100000 + 100;
 6 const double eps = 1e-9;
 7
 8 int n;
 9
10 struct Inter {
11     int le, ri;
12     Inter(int le = 0, int ri = 0) : 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[0].le + len;
20     if (pos > inter[0].ri + eps) return false;
21     for (int i = 1; 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 = 0; 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) / 2;
43             if (Judge(mid)) {
44                 ans = l = mid;
45             }
46             else r = mid;
47         }
48
49         int rp = 0, rq = 1;
50         for (int p, q = 1; 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 0;
60 }

转载于:https://www.cnblogs.com/npugen/p/9709343.html

UVA1616-Caravan Robbers(二分)相关推荐

  1. UVA1616 Caravan Robbers

    UVA1616 Caravan Robbers 题目链接 二分+小数转分数 题意:给定n个区间,把它们变成等长的不想交的区间,求区间的最大长度. 注意本题精度要求较高,注意浮点数的比较方式. 思路 1 ...

  2. UVA 1616 Caravan Robbers 【二分+贪心+枚举分母】

    题目链接 题意 给n个互不相包含的区间,求出一个长度的最大值,使得可以在每个区间中选出这样一个长度的子区间,这些子区间互不相交.结果用分数表示 分析 先考虑如果给定了区间长度能不能选出这样的区间.因为 ...

  3. 【二分】Caravan Robbers

    Caravan Robbers Long long ago in a far far away land there were two great cities and The Great Carav ...

  4. 习题8-14 商队抢劫者(Caravan Robbers, ACM/ICPC SEERC 2005, UVa1616)

    原题链接:https://vjudge.net/problem/UVA-1609 分类:二分法 备注:精度问题,技巧 #include<bits/stdc++.h> using names ...

  5. Uva 1616 Caravan Robbers (商队抢劫者)

    题意:给定 n 个区间,然后把它们变成等长的,并且不相交,问最大长度. 网上思路: 暴力二分枚举最大长度,判断是否可行. 我想的是贪心. now 表示当前已分配长度,s表示连续的一块的起始位置,cnt ...

  6. UVa 1616 商队抢劫者(Caravan Robbers)

    题意: 输入n条线段,把每条线段变成元线段的一条子线段,使得改变之后所有线段等长,且不相交.输出最大长度.例如有3条线段[2,6],[1,4],[8,12] 则最优方案变成 [3.5, 6] [1,3 ...

  7. Caravan Robbers CF Gym - 100134C

    https://cn.vjudge.net/problem/Gym-100134C http://codeforces.com/gym/100134/attachments 答案就是最小的min(bi ...

  8. UVa 1616 - Caravan Robbers

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. 《算法竞赛入门经典(第2版)》——学习记录

    前言:   这里主要记录本人在学习紫书过程中充分理解过的题目的AC代码,便于以后回顾时查找代码和思路,毕竟看别人的真的有点难懂.此外,本书甚至是本书之外的相关知识学习也可能在此留下记录.   作为一只 ...

最新文章

  1. android studio 一直在 svn performing vcs refresh
  2. 分享一个web页面背景全屏的jquery插件Fullscreen Background
  3. 编码练习——Java-数组-API-练习
  4. vue音乐笔记_Vue音乐项目笔记(二)
  5. 虽然有索引,但没有用上
  6. 关于一个js栈溢出的异常
  7. 搭建小程序表情包教程
  8. promise封装读取文件
  9. 活动地推怎么做最有效?
  10. Java:HttpClient篇,HttpClient4.2在Java中的几则应用:Get、Post参数、Session(会话)保持、Proxy(代理服务器)设置,多线程设置......
  11. 数学常用特殊符号读音
  12. java 批量修改图片名称_java 批量修改文件名称
  13. PHP 实现微信公众号网页授权登录
  14. 下载英文图书的几个网站
  15. 以太网交换机如何工作以及MAC和PHY
  16. Go语言判断接口的具体类型并进行类型转换
  17. 【饭谈】面试场上的珍珑棋局(PUA之无限打击)
  18. 2022年墙壁挂架/电视支架怎么选?四大专业电视支架品牌介绍
  19. Java项目:毕业论文管理系统(java+SSM+JSP+Tomcat+mysql)
  20. 北斗再入试题,北京中考物理实验【探究题】占比过半 方格教育

热门文章

  1. 两晋南北朝·高洋(上)
  2. 今天遇到一个问题,就是用pycharm运行python程序,老是会出现Python.exe已停止的对话框。
  3. 实验3 STP简单配置及介绍
  4. nodejs批量ping
  5. 【ARM嵌入式】实验报告一 熟悉RealView MDK集成开发环境的使用
  6. 磁盘管理关于磁盘的概念
  7. 家乡 二 十 四 桥的介绍
  8. linux cut指令
  9. 进阶项目(6)LCD12864液晶屏幕设计讲解
  10. Sign In with Apple - 使用苹果账号登录你的应用