挑战程序设计竞赛3.1例题:Cable master POJ - 1064

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
这道题看似简单,但是做起来还是有很多细节的:
1.精度问题,如果单纯每个可能性搜一遍那么cable最大到100000.00相当于要搜索1e7的量,每次搜索还要累加(这个是优化不了的)10000,相乘就是1e11的量,对于poj来说,差不多1000s才能完成,肯定不行,那么我们就要优化,对于前者,我们可以采用二分的方法将复杂度降低,对于log2100000约为17,也就是17次我们就能精确到个位,对于100次,我们完全就能精确到10-30这样的数量级(因为2-30约为7.8886*10-31),完全足够了。100*10000=1e7也完全符合要求。
2.对于取值问题,由于我们采用当中间值符合时将中间值赋给左值,不符合中间值赋给右值,实际上最后当精度为10-30的时候right - left < 10-30,问题来了,取左还是取右,由于两者基本上可以算作是相等(因为两者相差极小),但是考虑到只保留两位小数并且向下取整(因为如果四舍五入进位的话不可能截到这么长的),但是如果答案是1.99(输入样例:

3 6
1.99
3.98
5.97

left是1.9899999999999...而right是1.99000000000...1,那么对于left取舍会发生舍入成1.98造成错误,如果对left进行四舍五入,那么如果答案是1.53812321343,只能取1.53,left为1.53812...,四舍五入就是1.54就会发生错误,所以,为了规避这种错误,并且left和right相差很小,我们用right代替答案进行舍入,这样就可以规避出现1.98999999999舍入成1.98的情况,我们采用floor函数向下取整,那么我们先把right放大100倍在floor,两位进度后面的小数会被抹掉,这样就能输出正确答案了。

AC代码:
#include <stdio.h>
#include <math.h>
double cable[100005];
int n, k;
bool check(double num)
{
    int ans = 0;
    for(int i = 0; i < n; i++)
        ans += (int)(cable[i] / num);
    return ans >= k;
}
int main(void)
{

    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%lf", &cable[i]);
    double left = 0, right = 100005;//最大1e5,最小可能0
    for(int i = 0; i < 100; i++)
    {
        double mid = (left + right) / 2;
        if(check(mid))
            left = mid;
        else
            right = mid;
    }
        printf("%.2f\n", floor(right * 100) / 100);
    return 0;
}

  

 
posted @ 2020-02-18 09:23  funforever  阅读(199)  评论(0编辑  收藏  举报