您的位置:首页 > 其它

poj Cable master (二分+精度)

2016-05-18 20:32 141 查看
Cable master

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 35226Accepted: 7504
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段,问最大每段可以是多长 。
二分,开始想着不难,但是精度啊。。。弄了一下午。。。。还有里面的浮点,整型的替换,这次不记住都不行,还有是四舍不五入!!!!算个技巧吧!!值得一记!!

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;

double a[10010];
int n,k;
int judge(double x)//判断需要满足的条件
{
int ans=0;
for(int i=0;i<n;i++)
ans+=(int)(a[i]/x);	//!!!记住用法!记住啊!数据类型强制转换
if(ans>=k)
return 1;
else
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
double max=0.0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
if(max<a[i])
max=a[i];
}
double left,right,mid;
left=0.0;	//二分的范围
right=max;
while(right-left>1e-10)//最后求值有关精度问题,二分法的含义是找到左右逼近的数
{
mid=(left+right)/2;
if(judge(mid))
left=mid;
else
right=mid;
}
//输出right是根据题目的需要,来判断的
printf("%.2f\n",(int)(right*100)*1.0/100);//先强制转换再除
//用lf不行的,这也是个特殊情况吧。。。。
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: