poj 1064 Cable master(二分)

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

题意:给出N条线段,以米的单位给出,小数点后两位(精确到厘米),要你对这些线段裁剪,裁剪出K条等长的网线,并且让这些网线尽可能长另外网线的长度不能小于1厘米,如果筹不够K条,输出0.00
输入数据是double型,我们将其化为整数,一开始的二分区间就是[1,max{len[i]}] , 不要从0开始,完全没有比较,因为答案最小要为1

我们二分答案,得到一个长度mid,然后用所有线段去除mid,看能裁剪出多少条,然后统计条数的总和sum

然后就分类

sum < K,说明这个长度l太长,筹不够K条,所以要缩短长度mid,取左半区间二分

sum = K,说明是一个合法的答案,记录,但是这样就可以了吗?可以跳出了吗?不是的,因为要找最大值,所以不能跳出,继续到右半区间二分

sum > K , 这是不是一个合法的答案?其实是的!另外要到右半区间继续二分找一个更大的值

#include <iostream> #include <iomanip> using namespace std; int cable[10001],N,K,max1=0; bool cal(int length) {     int sum=0;     for(int i=0;i<N;i++)     {         sum=sum+cable[i]/length;         if(sum>=K)             return true;     }     return false; } int main() {     cin>>N>>K;     for(int i=0;i<N;i++)     {         double x;         cin>>x;         cable[i]=x*100;         if(cable[i]>=max1)             max1=cable[i];     }     int low=1,high=max1,mid=0,res=0;     while(high>=low)     {         mid=(low+high)/2;         if(cal(mid))             res=res>=mid?res:mid,low=mid+1;         else             high=mid-1;     }     cout<<setiosflags(ios::fixed)<<setprecision(2);     cout<<res/100.0<<endl;     return 0; }
 

你可能感兴趣的:(poj 1064 Cable master(二分))