二分查找应用分析(Cable master 2019年北大叉院机试原题--坑点精度问题)

查找一般应用:

  1. 查找,是从输入数据找到特定要求的数据
  2. 经常和排序结合,快速查找
  3. 在排序的基础上,二分查找是常见的题型

对于整型:

l=1,r=n;
while(l<r){
	int mid = l+(r+1-l)/2;
	if(a[mid]<q) l=mid;
	else r = mid-1;
}

对于浮点型:

double eps = 1e-5;//精度
	while(eps<r-l){
	double mid=(l+r)/2;
	if(check(mid))  l=mid;
	else  r= mid;
}

例题解析:

Cable master 2019年北大叉院机试原题 D - Cable master

试题内容:

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段,求K段网线的最大长度。
网线主管能够从库存的网线中切出指定数量的网线的最长长度(单位:米)。必须精确到厘米,即保留到小数点后两位。若无法得到长度至少为1cm的制定数量的网线,则必须输出“0.00”(不包含引号)。

解题分析:
题目考点:精度问题

对于精度问题需要知道的点:
//都是表示PI,但是结果却是不一样的, 不同的原因在于浮点计算会丢失精度

double a = asin( sqrt( 2.0 ) / 2 ) * 4.0;
double b = acos( -1.0 );
  output:a = 3.14159265358979360000;b = 3.14159265358979310000;
  		 a - b = 0.00000000000000044409

现在考虑一种情况,题目要求输出保留两位小数。有个case的正确答案的精确值是0.005,按理应该输出0.01,但你的结果可能是0.005000000001(恭喜),也有可能是0.004999999999(悲剧),如果按照printf(“%.2lf”, a)输出,那你的遭遇将和括号里的字相同。

第一种解决办法是,如果a为正,则输出a+eps, 否则输出a-eps
第二种方法:100次精度超级高,基本问题不大,算好复杂度无脑上就完事了
第三种就是用while(r-l>eps);

for(int i = 0; i < 100; ++i) {
        double mid = (l + r) / 2;
        if(check(mid)) l = mid;
        else r = mid;
    }
    return l;

题目坑点:保留两位小数,并不是四舍五入;

输出需要 printf("%.2lf",(int)(r*100)/100.0);

具体代码如下:

#include <cstdio>
#include <cmath>
#include <string>
using namespace std;
const int mMax = 10010;
int n,k;
double a[mMax];
double eps = 1e-3; //题目中说要保留后两位,因此精度至少要后推一位
bool check(double mid, int k){
	int num = 0;
	for(int i=0;i<n;i++){
		num += (int)(a[i]/mid);
	}
	if(num>=k) return true;
	else return false;
}
int main(){
	scanf("%d%d",&n,&k);
	double l=0,r=-1,mid=0; //r是数组a中最大的元素
	for(int i=0;i<n;i++){
			scanf("%lf",&a[i]);
			if(a[i]>r) r=a[i];
		}
	while(r-l>eps){
			mid =(r+l)/2.0;
			if(check(mid,k)) l= mid;
			else r=mid;
		}
	printf("%.2lf\n",(int)(r*100)/100.0);
	return 0;
}

勉励:
一切才刚刚开始,加油!ヾ(◍°∇°◍)ノ゙

如果觉得我的文章对你有所帮助与启发,点赞给我个鼓励吧(づ ̄3 ̄)づ╭❤~
关注我和我一起共勉加油吧!
如果文章有错误,还望不吝指教!

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值