集训笔记---二分法(POJ NO.1064 Cable master)

Cable master

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63997   Accepted: 13171

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

 目标在于寻找一个长度X让它能够满足条件求和(a[i]/x)的结果大于等于K,那么利用二分法进行枚举,然后采用是否满足sum >= k这个条件来决定lb,ub的取值问题就可以解决问题

#include<cstdio>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX_N = 100010;
int n, k;
double a[MAX_N];
bool C(double mid);
int main(void)
{
    int i;
    scanf("%d %d", &n, &k);
    for(i = 0; i < n; i++)
    {
        scanf("%lf", &a[i]);
    }
    double lb, ub;
    lb = 0;
    ub = INF;
    //设置二分搜索的上下限,ub取无穷大,目的在于枚举所有的可能情况 
    for(i = 0; i < 100; i++)//循环次数尽可能大让求出的长度无限逼近真正的结果 
    {
        double mid = (lb + ub) / 2;
        if(C(mid))//判断条件 
        {
            lb = mid;
        }
        else
        {
            ub = mid;
        }
    }
    printf("%.2lf\n", floor(ub*100)/100);//转换单位去除小数,再把单位换回来 
    return 0;
}
bool C(double mid)//判断条件是否满足的函数 
{
    int i, num;
    num = 0;
    for(i = 0; i < n; i++)
    {
        int h = (int)(a[i]/mid);
        num = num + h;
    }
    if(num >= k)
    {
        return true;
    }
    else
    {
        return false;
    }
}

扫描二维码关注公众号,回复: 2546345 查看本文章

猜你喜欢

转载自blog.csdn.net/zzuli_xiaomingke/article/details/81325964