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

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

时间: 2024-10-12 22:03:16

UVA1616-Caravan Robbers(二分)的相关文章

UVA - 1616 Caravan Robbers 二分+暴力转换

题目大意:给出n条线段,把每条线段变成原来线段的一条子线段,使得改变后的所有线段等长且不相交,输出最大长度 解题思路:这题要用long double确保精度,精度要求很高,接下来就是将这个long double的数转化为两个整数相除的结果了,有两种方法,学习到了 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define maxn 100010 #define esp

UVa 1616 Caravan Robbers (二分+贪心)

题意:给定 n 个区间,然后把它们变成等长的,并且不相交,问最大长度. 析:首先是二分最大长度,这个地方精度卡的太厉害了,都卡到1e-9了,平时一般的1e-8就行,二分后判断是不是满足不相交,找出最长的.这个题并不难, 就是精度可能控制不好,再就是把小数化成分数时,可能有点麻烦. 代码如下: #include <iostream> #include <cmath> #include <cstdlib> #include <set> #include <

UVA 1616 Caravan Robbers 商队抢劫者(二分)

依然是二分思路,但是精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差. #include<bits/stdc++.h> using namespace std; typedef long double ld; const int maxn = 1e5+5; const ld eps = 1e-11; struct Seg { int l,r; bool operator < (const Seg& x) con

UVa 1616 - Caravan Robbers

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4491 题意: 输入n条线段,把每条线段变成原线段的一条子线段,使得改变之后所有线段等长且不相交(端点可以重合).输出最大长度(用分数表示). 分析: 二分线段的长度,然后用贪心法检验可行性,最后把小数转换为分数.这样做虽然能AC,但这也是测试数据的问题.原因是贪心的方法不完全正确.

2.1 二分分类

本周学习神经网络编程的基础知识 构建神经网络,有些技巧是非常重要 神经网络的计算过程中,通常有一个正向的过程(正向传播步骤),接着会有一个反向步骤(反向传播步骤), 为什么神经网络的计算可以分为前向传播和反向传播两个分开的过程?本周课程通过使用logistic回归来阐述,以便于能够更好的理解, logistic回归是一个用于二分分类的算法 比如有一个二分分类问题的例子, 假如有一张图像作为输入是这样的,你想输出识别此图的标签,如果是猫,输出1,如果不是,则输出0 使用y来表示输出的结果标签, 来

HDU3715(二分+2-SAT)

Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3184    Accepted Submission(s): 1035 Problem Description Here is a procedure's pseudocode: go(int dep, int n, int m)beginoutput the valu

二分查找

递归版(在区间[x, y)中找v的位置) 1 //递归版二分查找 2 int bsearch(int * A, int x, int y, int v) 3 { 4 5 if(v<a[x] || v>a[y-1]) return -1; 6 int m = x + (y-x)/2; //此处能不能用int m = (x+y)/2,需要仔细考虑(暂时想不到原因) 7 if(A[m]==v) return m; 8 else if(A[m]>v) return bsearch(A, x, m

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

二分查找总结

最近刷leetcode和lintcode,做到二分查找的部分,发现其实这种类型的题目很有规律,题目大致的分为以下几类: 1.最基础的二分查找题目,在一个有序的数组当中查找某个数,如果找到,则返回这个数在数组中的下标,如果没有找到就返回-1或者是它将会被按顺序插入的位置.这种题目继续进阶一下就是在有序数组中查找元素的上下限.继续做可以求两个区间的交集. 2.旋转数组问题,就是将一个有序数组进行旋转,然后在数组中查找某个值,其中分为数组中有重复元素和没有重复元素两种情况. 3.在杨氏矩阵中利用二分查