Description
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
Output
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个线段给你一个m然后让你求的最长在这n段中可以截取》=m段长度
这一个就是二分,之前没有理解题意,也没有什么算法,就是二分。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 11000
int a[N];
int main()
{
int n, m, i;
double l;
while(scanf("%d%d",&n,&m)!=EOF)
{
int Max = 0;
for(i=0; i<n; i++)
{
scanf("%lf", &l);
a[i]=l*100.0;
Max = max(Max, a[i]);
}
int low = 1, high = Max;
int res = 0;
while(low <= high)
{
int mid = (low + high)>>1;
int count = 0;
for(i=0;i<n;i++)
count+=a[i]/mid;
if(count>=m)
{
res=max(res, mid);
low=mid+1;
}
else
high=mid-1;
}
printf("%.2f\n", (double)res/100.0);
}
return 0;
}