Caravan Robbers

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

题目大意

  • 给出一些区间,这些区间可能部分重叠,现请你求出一个最大的区间长度 lll,使得给出的所有区间都变成这个长度 lll 并且所有区间都不重叠。

思路

  • 二分答案
  • 一个关键点在于判断某个区间长度是否符合题意
  • 因为这道题的答案是用分数表示的,但是不能去二分分数,因为用分数的话会很复杂而且分子分母可能很大很大,所以只能二分小数,然后把小数转成分数,所以另一个关键点在于如何把这个小数变成分数。

判断某个区间长度是否符合题意:

  • 先排序区间,将起点从小到大排序,当起点一样时终点小的排在前面
  • 设置一个起始指针,从头开始遍历每个区间,如果这个指针小于当前区间的起点的话,就把这个指针拨到区间的起点,如果起始指针加上待判断的区间长度大于当前区间的终点时,就返回假,表示不符合题意;否则起始指针就拨到加上了待判断区间长度的位置。直到所有区间都判断完了,如果都行,那么就返回真。

把小数变成分数:

  • 二分答案出来了之后,我们需要把这个答案变成分数表示,即把这个小数表示为 p/qp/qp/q 的形式。
  • 设这个二分答案为 ansansans,就有ans=p/qans=p/qans=p/q;如果枚举分母 qqq,以此确定分子 qqq,并使得最后得到的 p/qp/qp/q 最接近 ansansans,那么最后就确定了 ppp和 qqq。
  • 这里记录一种很“巧妙”的方法。

代码如下

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define UIT unsigned int
#define DB double
#define PRII pair<int, int>
#define MMST(a) memset(a, 0, sizeof(a))
#define FOR(i, beg, ed, s) for (int i = beg; i < ed; i += s)
#define FORE(i, beg, ed, s) for (int i = beg; i <= ed; i += s)
#define LOOP while (true)
#define in scanf
#define out printf
using namespace std;const DB EPS = 1e-10;vector<pair<int, int>> aa;
int n;//判断某个区间长度是否符合题意
bool check(DB x) {DB st = 0;FOR(i, 0, n, 1) {if (st < aa[i].first) st = aa[i].first;if (st + x <= aa[i].second) {st += x;} else {return false;}}return true;
}void solve() {int a = 0, b = 0;DB r = 0, l = 0, mid = 0;LL p = 0, q = 0, ux = 0;//多组数据while (~in("%d", &n)) {aa.clear();//不要忘了清空容器FOR(i, 0, n, 1) {in("%d%d", &a, &b);aa.push_back(make_pair(a, b));}sort(aa.begin(), aa.end());l = 0, r = INT_MAX;while (r - l > EPS) {mid = l + (r - l) / 2.0;if (check(mid)) {l = mid;} else {r = mid;}}//这里得到二分答案l,并将其转为分数p = 0, q = 1;FORE(vx, 1, n, 1) {ux = round(l * vx);if (fabs((DB)ux / vx - l) < fabs((DB)p / q - l)) {p = ux, q = vx;}}out("%lld/%lld\n", p, q);}
}int main(void) {solve();return 0;
}

【二分】Caravan Robbers相关推荐

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

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

  2. UVA1616 Caravan Robbers

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

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

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

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

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

  5. Caravan Robbers CF Gym - 100134C

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

  6. UVa 1616 - Caravan Robbers

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

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

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

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

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

  9. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

最新文章

  1. java field setfont_Java JTextField.setFont方法代碼示例
  2. python程序在命令行执行提示ModuleNotFoundError: No module named ‘XXX‘ 解决方法
  3. 网络推广网站总结降低网站跳出率的技巧有哪些?
  4. 你必须知道的容器监控 (1) Docker自带子命令与Weave Scope
  5. php mysql query 行数_如何在PHP中获取MYSQL数据库返回的数据的行数?
  6. 【威佐夫博奕】 betty定理 poj 1067
  7. iOS的GIF动画效果实现
  8. 数据结构c语言版严蔚敏第二版课后答案
  9. 日记、2021/9/30
  10. 智能制造:三体智能革命
  11. 【滤波器】7. 带通滤波器
  12. linux设置cpu虚拟化,linux 查看cpu是否支持虚拟化
  13. 聊聊那些年遇到过的奇葩代码
  14. oracle自动清理归档,Oracle rman 自动清理归档日志
  15. aws rds mysql 连接_解决连接到 Amazon RDS 数据库实例的问题
  16. ​为什么冠状病毒的死亡率具有误导性?
  17. ecshop支付宝付款成功后台显示未付款
  18. php网页地图上自定义,网页嵌入百度地图和使用百度地图api自定义地图的详细步骤...
  19. STM32学习5——舵机控制
  20. 无线传感器网络原理及方法|无线传感器网络与应用|清华大学出版社-许毅|5th WEEK

热门文章

  1. win10锁定计算机后黑屏,win10锁定屏幕就黑屏怎么办
  2. 滴滴单通道语音分离与目标说话人提取和抑制技术进展
  3. 打印表格打印机没有反应_windows10下office2016文档和表格 hp打印机 按打印没反应解决办法...
  4. 关于LANDesk我们知道些什么
  5. mysql lbs_LBS类数据服务对比分析 (一)
  6. html 获取浏览器语言,js之获取浏览器语言
  7. 强网杯2018_core
  8. This system is not registered with an entitlement server.
  9. weboffice控件接收html文件,WebOffice 文档控件API
  10. android悬浮窗口 关闭,Android悬浮窗的创建及关闭