UVA1616-Caravan Robbers(二分)
Posted npugen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ?ghts 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 ?rst 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 ?le 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)
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 ", rp, rq); 58 } 59 return 0; 60 }
以上是关于UVA1616-Caravan Robbers(二分)的主要内容,如果未能解决你的问题,请参考以下文章
UVa 1616 Caravan Robbers (二分+贪心)
习题 8-14 UVA - 1616Caravan Robbers