Cable master
Time Limit : 2000/1000ms (Java/Other) Memory Limit :65536/32768K (Java/Other)
Total Submission(s) : 28 Accepted Submission(s) : 12
Problem Description
Inhabitants of the Wonderlandhave decided to hold a regional programming contest. The Judging Committee hasvolunteered and has promised to organize the most honest contest ever. It wasdecided to connect computers for the contestants using a"star" topology - i.e. connect them all to a single centralhub. To organize a truly honest contest, the Head of the Judging Committee hasdecreed to place all contestants evenly around the hub on an equal distancefrom it.<br><br>To buy network cables, the Judging Committee hascontacted a local network solutions provider with a request to sell for them aspecified number of cables with equal lengths. The Judging Committee wants thecables to be as long as possible to sit contestants as far from each other aspossible.<br><br>The Cable Master of the company was assigned to thetask. He knows the length of each cable in the stock up to a centimeter, and hecan cut them with a centimeter precision being told the length of the pieces hemust cut. However, this time, the length is not known and the Cable Master iscompletely puzzled.<br><br>You are to help the Cable Master, bywriting a program that will determine the maximal possible length of a cablepiece that can be cut from the cables in the stock, to get the specified numberof pieces.<br>
Input
The input consists of severaltestcases. The first line of each testcase contains two integer numbers N andK, separated by a space. N (1 ≤ N ≤ 10000) is the number of cables in thestock, and K (1 ≤ K ≤ 10000) is the number of requested pieces. The first lineis followed by N lines with one number per line, that specify the length ofeach cable in the stock in meters. All cables are at least 1 centimeter and atmost 100 kilometers in length. All lengths in the input are written with acentimeter precision, with exactly two digits after a decimal point.<br><br>Theinput is ended by line containing two 0's.<br>
Output
For each testcase write to theoutput the maximal length (in meters) of the pieces that Cable Master may cutfrom the cables in the stock to get the requested number of pieces. The numbermust be written with a centimeter precision, with exactly two digits after adecimal point.<br><br>If it is not possible to cut the requestednumber of pieces each one being at least one centimeter long, then the outputmust contain the single number "0.00" (without quotes).<br>
Sample Input
411<br>8.02<br>7.43<br>4.57<br>5.39<br>00<br>
Sample Output
2.00<br>
Source
2001-2002 ACM NortheasternEuropean Regional Programming Contest
题意:
有n条绳子,分成11段相等的,问能使得最长为多长。
解题思路:
直接二分答案,看二分到的值能否被剪成至少k断。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10000
#define exp 1.0e-9
int n,k;
double num[N],sum,result;
int Count(double len)
{
int count = 0;
for(int i = 0;i < n;i++)
count += int(floor(num[i] / len));
return count;
}
void SecondDiv()
{
double low,mid,high;
low = 0.0;
high = sum;
while(high - low > exp)
{
mid = (high + low) / 2;
if(Count(mid) >= k)
{
result = mid;
low = mid;
}
else
high = mid;
}
}
int main()
{
while(scanf("%d %d",&n,&k) && n + k)
{
sum = 0.0;
for(int i = 0;i < n;i++)
{
scanf("%lf",&num[i]);
sum += num[i];
}
result = 0.0;
SecondDiv();
printf("%.2lf\n",result);
}
return 0;