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

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 ("%.2f\n",floor(m*100)/100); //这个输出是因为%.2f输出会四舍五入,这样就可以避免四舍五入的问题,不能不少,否则会WA
    return 0;  
}  

另外在POJ上测试时,一定要使用%.2f进行输出,详情可见http://poj.org/showmessage?message_id=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 ("%.2f\n",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 ("%.2f\n",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 ("%.2f\n",(double)ans/100); //结果再除以100
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值