cable master POJ 1064

来源:互联网 发布:人工智能的未来 mobi 编辑:程序博客网 时间:2024/05/19 13:21

问题描述如下:

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 input consists of several testcases. The first line of each testcase contains two integer numbers 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 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.

The input is ended by line containing two 0's.
 

Output
For each testcase write to the output 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 must contain the single number "0.00" (without quotes).
 

Sample Input
4 118.027.434.575.390 0
 

Sample Output
2.00
分析, 采用二分搜索解决这个问题。 我们对于绳子的长度进行搜索, 二分搜搜的空间为(0, INF), 注意这里的INF对应的数不宜过大, 否则100次循环当然是不够的。 我曾经采用<cmath>中的 INFINITY表示INF, 结果100次的搜索远远不够, 得到错误的答案, 也采用1e32同理也不行的(注意e表示base10)。 另外输出的时候两位小数也要注意, 选择%.2lf 输出的结果是0.00(另外这个格式输出有四舍五入, 这也是不宜采用的原因), 当然是错误的。 尽管对于double来时, scanf输入的时候需要使用%lf, 但是输出的时候建议是用%.2f。  《挑战程序竞赛》这本书中说 100次二分搜索循环可以使得精度范围达到10^(-30)。 

二分搜索的过程, 每一次得到的x, 我们都检查这个值是否满足能够切割为K条相同长度的绳子。 也就是说用长度为x, 得到的Count条长度相同的绳子, Count  > K, 不难看出, 这个值也是最大的x。 程序如下:

#include <iostream>#include <climits>#include <cmath> // for floor function#include <cstdio>using namespace std;const int INF = 100000000;int N, K;double L[10010];bool check(double x) {    int Count = 0;    for(int i = 0; i < N; ++i) {        Count += (int)(L[i] / x);    }    return Count >= K;}int main() {    double lb = 0, ub = INF;//    freopen("..\\C-large.in","r",stdin);//    freopen("..\\C-large.out","w",stdout);    while(scanf("%d %d", &N, &K) != EOF) {        // for loop        for(int i = 0; i < N; ++i) {            scanf("%lf", &L[i]);        }//        double L[] = {8.02, 7.43, 4.57, 5.39};        double mid;        for(int i = 0; i < 100; ++i) {            mid = (lb + ub) / 2;            if(check(mid)) lb = mid;            else ub = mid;        }        printf("%.2f\n", floor(ub * 100) / 100);    }    return 0;}// a question// double d;//scanf("%lf", &d);//printf("%f", d);// C will promote floats to doubles for// functions that take variable arguments. Pointers aren't//promoted to anything, so you should be using %lf, %lg,//%le or %la (C99) to read in doubles

运行结果如下:



0 0