UVA1616-Caravan Robbers(二分)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值