Cable master(假定一个解并判断是否可行)(POJ NO.1064)

来源:互联网 发布:上海婚纱摄影推荐知乎 编辑:程序博客网 时间:2024/05/17 06:10

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 118.027.434.575.39

Sample Output

2.00

Source

Northeastern Europe 2001

题目大意:

有N条绳子,它们的长度分别为Li。如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留到小数点后两位。

分析:
这个问题用二分搜索可以非常容易的求得答案。
最优解的模板是:
“求满足某个条件C(x)的最小的x”
对于任意满足的C(x)的x,如果所有的x’>=x也满足C(x’)的话,我们就可以用二分搜索来求得最小的x。首先我们将区间的左端点初始化为不满足C(x)的值,右端点初始化为满足C(x)的值。然后每次取中点mid=(ub+lb)>>2,判断C(mid)是否满足并缩小范围,直到(lb,ub]足够小了为止。最后ub就是要求的最小值。最大化的问题也可以用同样的方法。

此处的条件C(x):=可以得到K条长度为x的绳子
则问题变成了求满足C(x)条件的最大x,在区间初始化的时候,只需要使用充分大的INF>(MaxL)作为上界即可:
lb=0;
ub=INF;
由于长度为Li的绳子最多可以切出floor(Li/x)段长度为x的绳子,因此
C(x)=(floor(Li/x)的总和是否大于等于K);

#include<cmath>double ceil(double x); ceil(x)返回不小于x的最小整数值(然后转换为double型)double floor(double x); floor(x)返回不大于x的最大整数值。double round(double x); round(x)返回x的四舍五入整数值。

如果这道题不用floor(ub*100)/100,那么直接输出”%.2lf”那么会直接四舍五入到两位(用cout<

AC代码:

#include<iostream>#include<algorithm>#include<vector>#include<cstdio>#include<string.h>#include<iomanip>#include<cmath>using namespace std;int N, K;const int Max_N = 10001;double L[Max_N];bool C(double x){    int cnt = 0;    for (int i = 0; i < N; i++)        cnt += int(L[i] / x);    return cnt >= K;}int main(){    while (~scanf("%d%d",&N,&K))    {        memset(L, 0, sizeof(L));        double maxl = 0;        for (int i = 0; i < N; i++)        {            scanf("%lf", L+i);            maxl = max(maxl, L[i]);        }        //for (int i = 0; i < N;i++)        //cout << L[i] << endl;        //二分        double lb = 0, ub =2*maxl;        double EPS = 0.001;        while ((ub-lb)>EPS)//或者用for(int i=0;i<100;i++)循环100次精度可达10e-30(0.5^100)        {            double mid = (lb + ub) / 2;            if (C(mid))lb = mid;            else ub = mid;        }        printf("%.2f\n", floor(ub*100)/100);    }}
0 0
原创粉丝点击