首页 > ACM题库 > HDU-杭电 > HDU 1551 Cable master-分治-[解题报告] C++
2013
12-12

HDU 1551 Cable master-分治-[解题报告] C++

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.

输入:

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.

输出:

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

样例输入:

4 11
8.02
7.43
4.57
5.39
0 0

样例输出:

2.00

题目大意:在做过hdu1969的前提下,这道题其实不需要知道什么意思就可以做了。。。。哈哈,我就是这样的。。如果需要明白是什么意思的

看一下我的hdu1969的解题报告就行了

解题思路:二分法

1)对于二分法、三分法的相关题目。精度的大小是一个很重要的问题。如何确定呢。。。我们可以这样思考:

根据题目给定的范围来确定。。如本题中有这么一句话"All cables are at least 1 centimeter and at most 100 kilometers in length"..所以精度应该是>=1e-8

代码如下:

/*
 * 1551_1.cpp
 *
 *  Created on: 2013年8月14日
 *      Author: Administrator
 */


#include <stdio.h>

int N,F;
double V[10001];
bool test(double x){
	int num = 0;
	int i = 0;
	for(i = 1 ; i <= N ;++i){
		num += int(V[i]/x);
	}

	if(num>=F){
		return true;
	}else{
		return false;
	}
}

int main(){

	while(scanf("%d%d",&N,&F),N||F){
		int i ;
		double sum = 0;
		for( i = 1 ; i <= N ; ++i){
			scanf("%lf",&V[i]);
			sum += V[i];
		}

		double max = sum/F;

		double l = 0;
		double r = max;
		while( r - l > 1e-10){
			double mid = (l+r)/2;
			if(test(mid)){
				l = mid + 1e-11;
			}else{
				r = mid - 1e-11;
			}
		}

		printf("%.2lf\n",(l+r)/2);


	}

}

解题报告转自:http://blog.csdn.net/hjd_love_zzt/article/details/9978987


  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。