#2020.01.14训练题解#二分入门(A题)

题源HDU-1551

HDU-1551-Cable master

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 input consists of several testcases. The first line of each testcase contains two integer numbers 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 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.
The input is ended by line containing two 0’s.

Output
For each testcase write to the output 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 must contain the single number “0.00” (without quotes).

Sample Input
4 11
8.02
7.43
4.57
5.39
0 0

Sample Output
2.00

题意

  • 给出N条线段,以米的单位给出,小数点后两位(精确到厘米)
  • 现在要对这些线段裁剪,裁剪出K条等长的线段
  • 并且让这些线段尽可能长另外线段的长度不能小于1厘米
  • 输出要精确到小数点后两位,如果筹不够K条,输出0.00

题解

  • 要精确到小数点后两位喔!!!为了完美逼近,需要开个eps精确量(如1e-5)
  • 对于输入的数据无需排序,因为得到的答案不一定在输入的数据值里
  • 初始记录最大值右边界再用其中一个数据分K段左边界即可
  • 因为这个每段的长度,满足所有数据分成K段,肯定也满足其中一个数据分成K段
  • solve函数判断记录能分的段数的时候,等于K的也要再将mid扩大尝试!!!
  • 因为答案小数K是整数
  • 11.09cm的线段分成11.05cm和11.08cm都是分出1段,K都是1
  • 而小数点后第二位是5还是8,就影响了题中的精确度
  • 这题只要对每段的长度进行二分预测,然后验证实际能不能分出K段这个长度
  • 如果不能分出,每段就要更小;如果可以分出,就试试更大的每段的长度能否拥有K段
  • 所以二分的对象每段的长度!!!每次只要验证这个长度能否满足题意即可啦
  • 【mid变量】 找分成多长
  • 【eps精确量】 实现题意精确度,尽量开小点叭,一般1e-5/1e-6的样子,太大可能wa
  • 【solve函数】 判断是否可以真的可以分mid长度分出K段
  • 如果人数满足,那么别忘了试试更大的数,毕竟是找最长每段能分出多长嘛(o゚▽゚)o

涉及知识点

  • 二分 算法(此题属于实数二分
  • 对于二分的算法-详见链接博客介绍二分

AC代码

#include<iostream>
using namespace std;
double put[10010];
double eps=1e-5; 
int n,k;//n是现有多少段电缆,循环输入;k是需要多少段电缆 
bool solve(double mid)
{
	long long num=0;
	for(int i=0;i<n;i++)
	{
		long long t=int(put[i]/mid);
		/*
			int()是C++的强制转换,(int)是C的强制转换 
			int()是一种单目运算符,此处运用相当于取整 
		*/ 
		num+=t;
	}
	if(num>=k) return 1;
	else return 0;
}
int main()
{
	while(~scanf("%d %d",&n,&k))//不停地输入现有段数 & 所需段数 
	{
		if(n==0&&k==0) break;
		double right=0.0;
		for(int i=0;i<n;i++)
		{//循环输入现有段数每段多长 
			scanf("%lf",&put[i]);
			if(put[i]>right) right=put[i];//right记录现有的最长段
		}
		double mid,left=put[0]/k;//left记录最小的元素 
		while(right-left>eps)//1的-5次方不一定,可依据题意开 
		{//二分被切成的长度,注意精确度 
			mid=(right+left)/2;
			if(solve(mid)) left=mid;//如果处理mid发现满足题意那再去找更大的值 
			else right=mid;//不满足solve就去找更小的值 
		}//循环终止时,left和right趋近相等,进行输出
		printf("%.2f\n",left);
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 0 · 访问量 406

猜你喜欢

转载自blog.csdn.net/qq_46184427/article/details/104073547