二分查找

该博客详细介绍了如何利用二分查找算法解决一个实际问题:在编程竞赛中,如何确定最大可能的电缆长度,以便将参赛者均匀分布在中央集线器周围。内容包括输入输出格式、算法实现以及处理精度问题的技巧。特别指出,由于浮点数精度限制,必须正确处理边界情况以避免误差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二分查找问题

Problem:
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

这道题直接用二分的方法,每一段cable的长度:最小是0,最大是这些cable的长度相加除于k。

1.二分:这里是寻找右侧边界的二分查找,因为越靠近0,越能达到所要求分的数,但是我们要能满足要求的最大的长度,所以是右侧边界二分查找。
2.精度:这道题特别坑的是精度问题。一些比较简单的,比如,while里的条件不能是左右相等中止啊,因为是double型,我们必须认定left和right相差小于一定值就当作相等了;然后还有这个题目要求割电缆绳的师傅只能割到0.01m的精度,所以最后用个小技巧,先把答案乘100,然后转换为int型可省掉后面的小数,再除于100.0转为float型,最后控制%.2f输出就好了。
但还有个巨大的坑,看这个链接写的第二点,由于double型在计算机里面二十进制的转换,1.99会变成1.989999999999……,这个时候如果最后取left和mid输出,都会变成1.98,所以要取right输出。

#include<stdio.h>
#include<math.h>
#define MINS 1e-8
int main(void){
	int n,k,i;
	double k_num[10001],sum=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++){
		scanf("%lf",&k_num[i]);
		sum += k_num[i];
	}
	double left,right,mid;
	left = 0;
	right = sum/k;
	while(fabs(right-left) > MINS){
	//	printf("left=%lf,right=%lf\n",left,right);
		mid = left + (right - left)/2;
		int pok = 0;
		for(i=0;i<n;i++){
			pok += (int)(k_num[i] / mid);
		}
		if(pok >= k){
			left = mid;
		}
		else if(pok < k){
			right = mid;
		}
	}
	right = (int)(right*100) / 100.0;
	printf("%.2f\n",right);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值