Cable master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3677 Accepted Submission(s): 1416
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.
The input is ended by line containing two 0's.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output must contain the single number "0.00" (without quotes).
4 11 8.02 7.43 4.57 5.39 0 0
2.00
题解:分的越长数量越少,分得越短数量越多。使得数量非常逼近需求量就是可以求出分得最长的情况。
对长度二分,判断分的段数
#include<cstdio>
#include<algorithm>
using namespace std;
double a[10010];
int n,k;
int dis(double x)
{
int sum=0;
for(int i=1;i<=n;i++)
{
sum+=(int)(a[i]/x);
if(sum>=k) //sum>=k时,已经是可能的情况退出,继续循环无意义还可能超时
break;
}
if(sum>=k) //sun<=k,分割太小,数量大,使mid变大,数量变小
return 1;
else
return 0;
}
int main()
{
while(~scanf("%d %d",&n,&k)&&(n&&k))
{
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
double l=0.0,r=100000.0; //最短0,最长100000
int size=50;
while(size--)
{
double mid=(l+r)/2;
if(dis(mid))
l=mid;
else
r=mid;
}
printf("%.2lf\n",l);
}
return 0;
}
415


&spm=1001.2101.3001.11974&articleId=52035883&d=1&t=3&u=0ca7d49d301e4974944fc566e0c27818)

被折叠的 条评论
为什么被折叠?



