【poj1064】Cable master(二分+精度注意)

来源:互联网 发布:淘宝优惠券制作 编辑:程序博客网 时间:2024/05/22 05:03
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. (小数点后是确定的两位数,不是保留两位小数emmm)
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 118.027.434.575.39
Sample Output
2.00
题意:输入n,k,给n个长度的电缆,需要截取成等长的K份,求所能截取的最大长度值,以m为单位保留2位小数。注意小于1cm输出0.00。

电缆长度的数据范围比较隐蔽:1m-100km(英语渣渣的我一直挖在这个地方orz)

用二分枚举每个可能的长度,尽量最大所以输出r。题中给的是m,全部转化成cm,这样就变成了整数,二分范围在1cm—1e9cm

注意输入浮点数的时候要加上0.005,我加的是0.0005(觉得这样更为接近输入的数),至于为什么要加上,我在调试的时候发现变量都比输入的要小。比如输入8.02,调试器里却是8.0199999999999..,既然大家都这样写,我也这样写;)

另外想强调的是强制类型转换(int)是去除小数部分,而floor是向下取整,差别就在于负数部分,比如(int)-3.5=-3,floor(-3.5)=-4。正数没区别。都不符合四舍五入。

而printf("%.2lf,a);保留两位小数,是会四舍五入的。所以解决办法是用floor或者(int),小数点后两个数printf("%.2lf,(int)(a*100)/100.0); 三个数printf("%.3lf,(int)(a*1000)/1000.0);

类似题目传送门:UVALive - 3635

讲完了上马~

解法1:浮点数储存(推荐)

#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<set>#include<map>typedef long long LL;double a[10005];int n,m;using namespace std;int orz(double x){    int i;    int sum=0;    for(i=1; i<=n; i++)        sum+=(int)(a[i]/x);    return sum;}int main(){    while(~scanf("%d%d",&n,&m))    {        int i;        double b;        for(i=1; i<=n; i++)        {            scanf("%lf",&b);            a[i]=b+0.0005;//要特别注意这个地方,否则wa        }        double l=0.0,r=100000.0,mid;        while(fabs(r-l)>=1e-5)        {            mid=(l+r)/2.0;            if(orz(mid)>=m)                l=mid;            else                r=mid;        }        printf("%.2f\n",(int)(r*100)/100.0);//小数点后是两位确定的数字,不可四舍五入    }    return 0;}


解法2题中给的是m,全部转化成cm,这样就变成了整数,二分范围在1cm—1e9cm

#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<set>#include<map>typedef long long LL;int a[10005];int n,m;using namespace std;int orz(int x){    int i;    int sum=0;    for(i=1; i<=n; i++)        sum+=(int)(a[i]/x);    return sum;}int main(){    while(~scanf("%d%d",&n,&m))    {        int i;        double b;        for(i=1; i<=n; i++)        {            scanf("%lf",&b);            a[i]=(b+0.0005)*100;        }        int l=1,r=1e9,mid;        while(l<=r)        {            mid=(l+r)>>1;            if(orz(mid)>=m)                l=mid+1;            else                r=mid-1;        }        printf("%.2f\n",r/100.0);    }    return 0;}





原创粉丝点击