1. 程式人生 > >Cable master 小數二分

Cable master 小數二分

Cable master
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36054 Accepted: 7693

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

Source

    該題使用二分法解決,但是需要注意精度。這精度……實在讓人頭疼。

    題意:有n根電線,你需要從中剪得k根同等長度的電線,現在求剪得電線的最大值。

    分析:逆向思維用最長長度去驗證是否滿足條件能剪得不小於k的根數。驗證函式很容易理解,由於電線之間不可以拼接,所以每根電線長度/割成電線的長度即為此根電線可形成所求電線的根數,列舉即可得到最終可割個數。二分法判斷在此不贅述。

    給出能在pojAC的程式碼:

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=10005;
double a[maxn];
int n,k;
int check(double x)  //判斷函式
{
	int num=0;
	for(int i=0; i<n; i++)
		num+=(int)a[i]/x;  //int 不可少
	if(num>=k) //大於等於是根據題意  要讓結果儘可能大
		return 1;
	return 0;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0; i<n; i++)
		scanf("%lf",&a[i]);
	sort(a,a+n); //排序確定 r的值 該題中 每根線的值不可能超過最大的電線長度
	double l=0,r=a[n-1],mid,res=0.00;
	while(r-l>=1e-15)  //精度
	{
		mid=(l+r)/2;
		double p=check(mid);
		if(p)
		{
			res=mid;  //注意更新答案
			l=mid;
		}
		else
			r=mid;
	}
	if((int)(res*1000)%10>=5)
		r-=0.005;
	printf("%.2lf\n",res);
}
    該題最為坑人的一點是,題目要求的結果是兩位小數的,並不能單單地printf("%.2lf\n",res),這樣是預設四捨五入的,舉例:所得結果是3.457,題目要求的答案是3.45,而直接printf得到的是3.46!wa來wa去的……所以需要對最終的結果進行處理,乘以一千對10取餘後若大於5則該種情況會四捨五入,原結果減去0.005即可。真是……

    該題花了很多時間,看來讀題得謹慎,不能想當然。

    特記下,以備後日回顧。