码迷,mamicode.com
首页 > 其他好文 > 详细

UVA1616-Caravan Robbers(二分)

时间:2018-09-26 20:47:56      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:BMI   car   rip   leader   要求   abs   rup   The   题解   

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 ?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

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 ?rst 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 }

 

UVA1616-Caravan Robbers(二分)

标签:BMI   car   rip   leader   要求   abs   rup   The   题解   

原文地址:https://www.cnblogs.com/npugen/p/9709343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!