poj 1064 Cable master

来源:互联网 发布:淘宝童鞋运动少女 编辑:程序博客网 时间:2024/06/05 09:28

这几天做了题才知道,二分法不止可以用于查找尴尬二分搜索法,是通过不断缩小解可能存在的范围,从而求得问题的最优解的方法,这个算法在求最优解的问题上非常有用。让我们考虑一下,“求满足某个条件C(x)的最小的x"这一问题。对于任意满足C(x)的x,如果所有的x'>=x也满足C(x')的话,我们就可以用二分搜索来求得最小的x。首先我们将区间的左端点初始化为不满足C(x)的值,右端点初始化为满足C(x)的值。然后每次取中点mid=(first+last)/2,判断C(mid)是否满足并缩小范围,直到(first,last]足够小为止。最后last就是要求的最小值。最大化问题也可以用同样的方法求解

/*DescriptionInhabitants 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.InputThe 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.OutputWrite 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 Input4 118.027.434.575.39Sample Output2.00题意:有n条绳子,他们的长度分别为L,如果从他们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有长?答案保留到小数点后2位1<=N<=100001<=K<=100001<=li<=100000*/#include<stdio.h>#include <string.h>#include <math.h>#define Max(a,b) a>b?a:bdouble l[100005];int k,n;bool fun(double x){int i,num=0;for(i=0;i<n;i++){num+=(int)(l[i]/x);}if(num>=k)return true;elsereturn false;}int main(){int i;double mid,max;while(scanf("%d%d",&n,&k)!=EOF){max=-1;for(i=0;i<n;i++){scanf("%lf",&l[i]);max=Max(l[i],max);}double first=0;double last=max;for(i=0;i<100;i++){mid=(first+last)/2;if(fun(mid))first=mid;//如果符合条件,就尽可能的往右边搜索最大值 elselast=mid;}printf("%.2lf\n",floor(last*100)/100);//double floor(double x)求不大于x的最大整数 }return 0; } 


0 0