【计算理论与算法分析设计】 3. Cable master (POJ

阅读: 评论:0

【计算理论与算法分析设计】 3. Cable master (POJ

【计算理论与算法分析设计】 3. Cable master (POJ

Cable master

Description

(这一节不用看)
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a “star” topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Input

The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 <= N <= 10000) is the number of cables in the stock, and K (1 <= K <= 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.

Output

Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number “0.00” (without quotes).

Sample Input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

Source

Northeastern Europe 2001

题目描述

给了你N条电缆(1 <= N <= 10000),它们的长度都是精确到厘米的,最短为1cm,最长为100km。将其以厘米为精度切成等长的K段(1 <= K <= 10000),问能切出来的最长的长度是多少。

问题分析

这道题的思路肯定是去枚举长度,然后看每一条电缆能被切成几段,找出来满足题目设定的段数的最大长度。但是怎么去枚举,是一个需要思考的问题。暴力遍历肯定会超时,不可取,所以想到了二分法。

初始的下界l为0,上界r为最长的那个电缆的长度,在这个区间内进行二分,由于是浮点数,所以要使用while (r-l>eps)这样的形式而不能直接用运算符去判断大小。如果当前长度切割出来的段数小于K,说明长度大了,应该到更小的区间内寻找;否则说明长度小了,应该到更大的区间内寻找。最后while循环退出时,就找到了最长的长度。

思路还是很简洁易懂的,但接下来就是玄学的地方了……这是玄学与非玄学的分界线↓


这题在精度上真是卡得要了命了……我直接粘贴了当时寒假集训时写的代码,然后发现我二分eps取的是1e-10,但是最后二分结束之后,我给m(就是中间值)加了个1e-8,然后再输出的。。。我真的看不懂我当时都是什么玄学操作。。。不加就WA,加上就AC。。。

然后我到其他博客里面研究,发现有人加了0.005,有人减了0.00499……都是玄学操作。。。还有输出l,m还是r,都各不相同,各显神通。。。还有人放弃了利用r-l>eps来控制循环是否结束,直接暴力二分100次得出结果。。。实在是搞不清楚了。。。

总结:二分如果WA的话,如果是精度上面出的问题,就尝试加或者减一些东西,然后尝试把输出改一改,l,m或r都试一下,说不定就对了……

下面的AC代码是如上所述的几个思路。

AC代码

1. 本人的代码

就是那个玄学+1e-8……

#include <cstdio>  
#include <cmath>  
int main()  
{  long long i,n,k;  scanf ("%lld%lld",&n,&k);  double a[10005],m,l=0.0,r,max=-1.0;  for (i=0;i<n;i++)  {  scanf ("%lf",&a[i]);  if (a[i]>max) max=a[i]; //寻找最大的电缆长度 }  r=max;  while (r-l>1e-10) //浮点数二分条件要这样设置 {  long long s=0; //s统计切割出的总条数m=(r+l)/2;  for (i=0;i<n;i++)  {  s+=(long long)(a[i]/m);  }  if (s>=k) l=m; //条数多了,则说明当前考虑的长度偏小 else r=m;  //否则长度偏大}  m+=1e-8;  //玄之又玄,众妙之门printf ("%.2fn",floor(m*100)/100); //这个输出是因为%.2f输出会四舍五入,这样就可以避免四舍五入的问题,不能不少,否则会WAreturn 0;  
}  

另外在POJ上测试时,一定要使用%.2f进行输出,详情可见=126922

2. 同学的思路

课下和同学交流时,得知eps使用1e-4,并且输出改成r(你看看,玄之又玄吧),也能通过。我尝试了一下果然如此。代码如下:

#include <cstdio>
#include <cmath>
int main()
{long long i,n,k;scanf ("%lld%lld",&n,&k);double a[10005],m,l=0.0,r,max=-1.0;for (i=0;i<n;i++){scanf ("%lf",&a[i]);if (a[i]>max) max=a[i];}r=max;while (r-l>1e-4){long long s=0;m=(r+l)/2;for (i=0;i<n;i++){s+=(long long)(a[i]/m);}if (s>=k) l=m;else r=m;}printf ("%.2fn",floor(r*100)/100); //玄之又玄,众妙之门return 0;
}

3. 暴力二分100次

这个我也尝试了一下,也是可行的(玄学,哪有这么二分的)。代码如下:

#include <cstdio>
#include <cmath>
int main()
{long long i,n,k;scanf ("%lld%lld",&n,&k);double a[10005],m,l=0.0,r,max=-1.0;for (i=0;i<n;i++){scanf ("%lf",&a[i]);if (a[i]>max) max=a[i];}r=max;for (int j=1;j<=100;j++) //玄之又玄,众妙之门{long long s=0;m=(r+l)/2;for (i=0;i<n;i++){s+=(long long)(a[i]/m);}if (s>=k) l=m;else r=m;}printf ("%.2fn",floor(m*100)/100);return 0;
}

4. 把电缆长度同乘100,转化为整数二分

这个是当时请教学长,学长提供的思路。当时没有去尝试,现在写博客的时候试了一下,也是可以的。并且,浮点数二分转化为整数二分,是一个很好的避免精度问题的手段。但是,转化为整数之后,需要多考虑一些问题。详见代码。

#include <cstdio>
#include <cmath>
int main()
{long long i,n,k,a[10005],l=0,m,r,max=-1,ans=0;scanf ("%lld%lld",&n,&k);double t;for (i=0;i<n;i++){scanf ("%lf",&t);a[i]=(long long)(t*100); //同乘100转化为整数二分if (a[i]>max) max=a[i];}r=max;while (l<=r) //整数二分的终止条件{long long s=0;m=(r+l)/2;if (r==0||m==0) break; //如果有一个为0,代表不能分成大于1cm的段,这里不管的话会出现浮点运算错误(除0)for (i=0;i<n;i++){s+=a[i]/m;}if (s>=k){ans=m; //记录当前的最优解,二分的模板l=m+1; //更新l}else r=m-1; //更新r}printf ("%.2fn",(double)ans/100); //结果再除以100return 0;
}

本文发布于:2024-01-28 23:08:39,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170645452010961.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:算法   理论   POJ   master   Cable
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23