Language:Default 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. 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. Sample Input Sample Output |
二分水题, 精度要求较高, 结果也需要处理一下防止浮点误差造成的四舍五入
#include<cstdio>
#include<algorithm>
using namespace std;
double a[10005];
int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
double sum = 0;
for(int i = 0; i < n; ++i)
{
scanf("%lf", &a[i]);
sum += a[i];
}
double l = 0, r = sum/k;
for(int i = 0; i < 100; ++i)
{
double mid = (l + r) / 2;
long long num = 0;
for(int i = 0; i < n; ++i)
num += a[i] / mid;
if(num >= k)
l = mid;
else r = mid;
}
printf("%.2f\n", floor(l * 100)*0.01);
}
return 0;
}