Cable master·二分

题目信息

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.
仙境的居民们决定举办一场程序设计区域赛。裁判委员会完全由自愿组成,他们承诺要组织一次史上最公正的比赛。他们决定将选手的电脑用星形拓扑结构连接在一起,即将它们全部连到一个单一的中心服务器。为了组织这个完全公正的比赛,裁判委员会主席提出要将所有选手的电脑等距离地围绕在服务器周围放置。
为购买网线,裁判委员会联系了当地的一个网络解决方案提供商,要求能够提供一定数量的等长网线。裁判委员会希望网线越长越好,这样选手们之间的距离可以尽可能远一些。
该公司的网线主管承接了这个任务。他知道库存中每条网线的长度(精确到厘米),并且只要告诉他所需的网线长度(精确到厘米),他都能够完成对网线的切割工作。但是,这次,所需的网线长度并不知道,这让网线主管不知所措。
你需要编写一个程序,帮助网线主管确定一个最长的网线长度,并且按此长度对库存中的网线进行切割,能够得到指定数量的网线。

输入

The first line of the input file contains two integer numb ers N and K, separated by a spaceN(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.
第一行包含两个整数N和K,以单个空格隔开。N(1 <= N <= 10000)是库存中的网线数,K(1 <= K <= 10000)是需要的网线数量。
接下来N行,每行一个数,为库存中每条网线的长度(单位:米)。所有网线的长度至少1m,至多100km。输入中的所有长度都精确到厘米,即保留到小数点后两位。

输出

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).
网线主管能够从库存的网线中切出指定数量的网线的最长长度(单位:米)。必须精确到厘米,即保留到小数点后两位。
若无法得到长度至少为1cm的指定数量的网线,则必须输出“0.00”(不包含引号)。

测试样例

测试样例1

4 11
8.02
7.43
4.57
5.39
2.00

测试样例2

1 10000
100000.00
10.00

来源

POJ1064 -> Northeastern Europe 2001

解答

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int N;
int K;
double Num[10000];

bool check(double x)
{
    
    
    int n = 0;//能产生几根整数电线
    for (int i = 0; i < N; i++)
    {
    
    
        n += (int) (Num[i] / x);
    }
    if (n >= K)
    {
    
    //大于产生的电缆个数了
        return true;
    }
    else
    {
    
    
        return false;
    }
}

int main()
{
    
    
    //freopen("E://test.txt", "r", stdin);
    cin >> N >> K;
    double Num_sum = 0;
    for (int i = 0; i < N; i++)
    {
    
    
        cin >> Num[i];
        Num_sum += Num[i];
    }
    double left = 0;
    double right = Num_sum / K;
    double mid;
    while (right - left >= 1e-8)
    {
    
    //两数不相等
        mid = (right + left) / 2;
        if (check(mid))
        {
    
    //表示产生的数目多了,表示应该取大于mid的部分
            left = mid;
        }
        else
        {
    
    
            right = mid;
        }
    }
    cout << setiosflags(ios::fixed) << setprecision(2) << floor(right * 100) / 100 << endl;
    return 0;
}

想法

注意四舍的方法,用floor来实现。
还有本题的正确做法应该是将米转换成厘米,使用int的方式来处理,这样才会得到一个完美的解答,使用double很难处理处理精度,最后答案给出的使用right也能通过样例其实是一个投机取巧的方式。

猜你喜欢

转载自blog.csdn.net/zhj12399/article/details/113127371
今日推荐