poj1064 Cable master(二分)

Cable master
Time Limit: 1000MS Memory Limit: 10000K
   

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

题意:

有N断电缆,长短不一,要求分成K份,使得每一份最长。


思路:

按照传统思路,将最长的那一段电缆二分,判断每份长mid是否能分成K份,这和之前写过poj3122的思路一模一样。。。


处理浮点数的方法:

这题只精确到0.01,那么一个处理对策就是将二分得到的最终答案mid乘以100强制转换为int,舍弃后面的小数位,再除以100

此外,还有可能出现(int)(0.03*100)等于2的情况,我在自己电脑上试了输出为3,但同学说他的输出为2,所以还需要加上一个eps,可以写出这样(int)(0.03*100+eps)


另外一个处理对策就是将输入的所有的double转换成整型来写,注意用long long




总结:

浮点数一生坑系列。。。

开始上来就看了这题,思路也很清晰,我很快就开始敲了,重看了一下榜,15分钟左右,我和另一个同学的交了,结果都WA了。

接下来成功进入了浮点数之莫名其妙WA的阶段。

比赛提醒尽量用c++,我平常也用cin, cout,但是比赛时出了个问题,忘记c++浮点数输出的格式了,连头文件都忘了,于是只好用c的输入输出格式。。


第一次交WA,发现没有判断输出为0.00的情况

改了交,继续WA

后面又给了两组数据


发现了错误后改了再交,仍然WA


前前后后,从怀疑自己看错题,到怀疑二分的正确性,到怀疑没用cin, cout导致错误,结果比赛结束了都没AC,虽然WA了几次后就开始看另一题,而且发现了另一题能写就果断先放了这题,但结果是两题都写的不顺利。。



注意我是用c++交的,用G++ TLE,暂时不懂原因


代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;
double len[10010];
int N, K;
const double eps = 1e-7;

bool check(double mid){
    int cnt = 0;
    for(int i=0; i<N; i++)
        cnt += ((int)(len[i]/mid));
    if(cnt>=K)
        return false;
    else
        return true;
}

int main(){

    while(cin>>N>>K){
        double sum = 0;
        double maxx = -1.0;
        for(int i=0; i<N; i++){
            cin>>len[i];
            maxx = max(maxx, len[i]);
            sum += len[i];
        }


         double tmp = 0.01*K;
         if(tmp-sum>eps){
             cout<<"0.00"<<endl;
             continue;
         } //判断能否满足条件,我开始用除法,后面改成乘法

        double l = 0;
        double r = maxx;
        double mid;
        while(fabs(l-r)>=eps){
            mid = (l+r)/2;
            if(!check(mid))
                l = mid;
            else
                r = mid;
        }
        double ans = ( ((int)(mid*100.0+0.0001)) )/100.0;
        cout<<fixed<<setprecision(2)<<ans<<endl;
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值