热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:UVA1616CaravanRobbers(二分)

本文由编程笔记#小编为大家整理,主要介绍了UVA1616-CaravanRobbers(二分)相关的知识,希望对你有一定的参考价值。
本文由编程笔记#小编为大家整理,主要介绍了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

 
















技术分享图片 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
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 ) {
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 ) {
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) 1.0*rp / rq - ans)) {
53 rp = p, rq = q;
54 }
55 }
56
57 printf("%d/%d
", rp, rq);
58 }
59 return 0;
60 }

 










推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
author-avatar
雪染苍原牛仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有