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

——————————————————————————————————————————————

解题思路

对于给定的几条电缆(位于仓库中),相互不可以拼接,而要截取出给定段数下最长的电缆
如果能够获得合理的长度(>0),那么输出即可,如果不可以则输出0

二分治问题,需要对于所有的电缆遍历处理,以方便判断特定长度能否裁出符合条件的电缆条数。如果是不可拼接(如题意的情况),二分点的右端点取到最长的一条电缆即可(假设是可以拼接的电缆,那么拼接起来之后求即可)

P.S: 必须从最大最长的那条电缆开始二分。当然不可以从最小的最短的那一条开始二分,如果最短的电缆作为右端点,假设存在所需电缆条数小于仓库电缆条数的情况,那么答案必定会比求得的答案更大,还有其他一些问题等等。

采用整数写法,核心在于不需要判断小数点第三位的数字
注意题目的厘米和米单位变化和数组大小的区别,避免 mletle

#include<stdio.h>
int stock[10010] = {'\0'}; 
int num_insto, num_needed; //前者是仓库中的电缆段数 后者是需要的电缆段数 
/*
	用于判断二分法结果所得数字是否符合条件的函数
	TCN是tmp_cable_number 
*/
bool num_counting(int TCN){  
	
	int count = 0;
	for(int i = 0; i < num_insto; i++){
		
		count += stock[i]/TCN; 
	} 
	//判断该数字是否符合最低的所需电缆条数 
	
	if(count >= num_needed)	return true; 
	else
		return false;
} 

int main(void){
	
	scanf("%d %d", &num_insto, &num_needed);
	
	int maxn = -1;
	for(int i = 0; i < num_insto; i++){ //输入数据的同时进行统计 
		
		float tmp;
		scanf("%f", &tmp);
		tmp = (int)(tmp*100); //把厘米换成米
		/*
			在进行二分法的时候
			二分法的起点不会超过最长的那一条 
		*/
		if(maxn <= tmp) maxn = tmp; //找到最长的一条 
		stock[i] = tmp;
	}
	/*
		算法实现 
		在[0,maxn]中寻找最长的且符合条件的 
	*/ 
	/* 测试 
		for(int i = 0; i < num_insto; i++){
		
			printf("%d ", stock[i]);
		} 
		printf("\n%d\n", maxn);
	*/
	int left = 0, right = maxn;
	int ans = 0, middle, flag = 1; 
	while(left <= right){
		
		middle = (left + right)>>1;
	//	printf("%d\n", middle);  for a test
		/*
			>>1的含义 = /2 
		*/ 
		if(right == 0 || middle == 0){
			
			flag = 0; //说明不行 
			break;
		}
		/*
			如果长度合适,太短,那么从左起点增加一位
			如果长度不合适,太长,那么从右起点减少一位 
		*/
		if(num_counting(middle)){
			
			ans = middle;
			left = middle + 1;
		} 
		else
			right = middle - 1;
	}
	if(!flag)
		printf("0.00\n");
	else 
		printf("%.2f\n", (float)ans/100);
	return 0;
}

发布了2 篇原创文章 · 获赞 0 · 访问量 35

猜你喜欢

转载自blog.csdn.net/weixin_43778198/article/details/104464723