Challenges Programming Contest 3.1 example: 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 
This question may seem simple, but it is still a lot of details of:
1. the accuracy problem, if you simply search every possibility that cable again to search up to 100000.00 equivalent amount of 1e7, each search also accumulate ( this is not optimized) 10000, is multiplied by the amount of 1e11, for the poj for almost 1000s to complete, certainly not, then we will optimize for the former, we can adopt the method of dichotomy reduce complexity, for log 2 100000 of about 17, that is accurate to 17 bits we can, for 100 times, we can fully accurate to 10 -30 of such magnitude (because the two -30 of about 7.8886 x 10 -31 ), fully adequate a. 100 * 10000 = 1e7 also fully meet the requirements.
2. For the value of the problem, since we use when intermediate values in line with the value assigned to the left middle value, does not meet the intermediate value is assigned to the right value, when in fact the final accuracy of 10 -30 when the right - left < 10 -30 , the question is, take the right or left to take, since both can be regarded as substantially equal (because of a difference of a minimum), but considering only two decimal places and is rounded down (as if the words do not carry rounding possible to cut that long), but if the answer is 1.99 (sample input:

3 6
1.99
3.98
5.97

left and right are 1.9899999999999 ... is 1.99000000000 ... 1, then for left-offs occur rounded to 1.98 caused the error, if left rounded, so if the answer is 1.53812321343, can only take 1.53, left to 1.53812 ... rounding error occurs is 1.54, so, in order to avoid this error, and left and right difference is small, we use the right answer instead of rounding, so you can avoid the situation appears 1.98999999999 rounded to 1.98, we use the floor function rounded down, then we put right magnified 100 times in the floor, behind the progress of two decimal places will be erased, so you can output the correct answer. 

AC Code:
#include <stdio.h>
#include <math.h>
double cable[100005];
int n, k;
bool check(double num)
{
    int years = 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; // maximum 1e5, the smallest possible 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;
}

  

 

Guess you like

Origin www.cnblogs.com/jacobfun/p/12324845.html