UVa 1616 - Caravan Robbers

链接:

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

题意:

输入n条线段,把每条线段变成原线段的一条子线段,使得改变之后所有线段等长且不相交(端点可以重合)。
输出最大长度(用分数表示)。

分析:

二分线段的长度,然后用贪心法检验可行性,最后把小数转换为分数。
这样做虽然能AC,但这也是测试数据的问题。原因是贪心的方法不完全正确。
无论是按线段的左端点排序,还是按右端点排序,都能找到出错的例子(见下面的测试数据)。
目前网上几乎所有关于这道题的题解都有这个问题,所以这道题还没解决。。。

代码:

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 struct TYPE {
 7     int f, b;
 8     bool operator < (const TYPE& that) const {
 9         return b < that.b;
10     }
11 } a[100000+5];
12
13 int main(){
14     int n;
15     while(~scanf("%d", &n)){
16         for(int i = 0; i < n; i++) scanf("%d%d", &a[i].f, &a[i].b);
17         sort(a, a + n);
18
19         double L = 0, R = 1000000; //二分法
20         while(R - L > 1e-9){
21             int i;
22             double M = L + (R - L) / 2, pos = 0;
23             for(i = 0; i < n; i++){ //贪心法
24                 if(pos < a[i].f) pos = a[i].f;
25                 if(pos + M > a[i].b) break;
26                 pos += M;
27             }
28             if(i == n) L = M;
29             else R = M;
30         }
31
32         int ac = 0, ad = 1; //小数转分数
33         for(int d = 1; d <= n; d++){
34             int c = round(L * d);
35             if(fabs(1.0*ac/ad - L) > fabs(1.0*c/d - L)) ac = c, ad = d;
36         }
37         printf("%d/%d\n", ac, ad);
38     }
39     return 0;
40 }

测试数据:

input1:

2
0 7
3 6

output1:

3/1

input2:

2
1 6
2 4

output2:

2/1

原文地址:https://www.cnblogs.com/hkxy125/p/8543527.html

时间: 2024-10-28 21:12:37

UVa 1616 - Caravan Robbers的相关文章

UVa 1616 Caravan Robbers (二分+贪心)

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

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 商队抢劫者(二分)

依然是二分思路,但是精度要求很高需要用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 361 - Cops and Robbers(凸包)

题目中给出了n个cops和m个robbers和q个居民,如果一个居民在某三个cops围成的三角形中就是安全的,否则,如果在某三个robbers围成的三角形中,就是不安全的,不然就是neither. 思路:这个可以转换成凸包来做.判断某个居民是不是在某个凸包内部就行了. :下面是凸包的求法之一 int getConvexHull (Point* p, int n, Point* ch) { sort(p, p + n); // for (int i = 0;i < n;++i) // p[i].w

UVA 563 Crimewave (最大流,拆点)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=504  Crimewave  Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangula

uva 1486 Transportation (最大流+拆边)

uva 1486 Transportation Description There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry,

UVA1616-Caravan Robbers(二分)

Problem UVA1616-Caravan Robbers Accept: 96  Submit: 946Time 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. B

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te