POJ1064⭐

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.


输入文件的第一行包含两个整数N和K,用一个空格分隔。其中N (1 = N = 10000)为库存线缆数量,K (1 = K = 10000)为需求数量。第一行后面是N行,每行一个数字,指定库存中每根电缆的长度(以米为单位)。电缆长度不小于1m,不大于100km。输入文件中的所有长度具有精确到厘米,与小数点后两位数字。


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


在输出文件中写入电缆管理员可以从库存电缆中切割的最大长度(以米为单位),以获得所需数量的电缆。该数字必须以厘米精度书写,小数点后必须正好有两位数字。
如果不能裁剪所需数量的片段,每个片段至少一厘米长,那么输出文件必须包含单个数字“0.00”(不带引号)。


Sample Input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

题目摘要

有N根电缆,它们长度分别为Len[i]。若要从从N根电缆中切割出 K条 长度相同的电缆,那么这K条绳子 每条最长能有多长\rightarrow最大化最小值问题

解题思路

用二分法来找出答案。(二分范围明确且思路清晰)

对长度进行二分,可以找到一个ans,<ans一定可以分为K条,>ans不够分为K条。

那么如何找到找到这个ans?\rightarrow判断条件为int P(double mid)

int P(double mid)
{
	int sum = 0;
	for (int i = 0;i < N;i++)
	{
		sum += Len[i] / mid;//sum统计在当前mid(长度)下可以得到的电缆数目
	}
	return sum>=K;//如果sum>=k,说明mid<=ans,则L=mid;如果sum<k,说明mid>ans,则R=mid
}

注意点

保留小数点后两位

上代码

#include<iostream>
#include<cstdio>
using namespace std;

int N, K;
double Len[10010];

int P(double mid)
{
	int sum = 0;
	for (int i = 0;i < N;i++)
	{
		sum += Len[i] / mid;
	}
	return sum>=K;
}

int main()
{
	double L = 0;
	double R = 1e5 + 1;
	double mid;

	scanf("%d%d", &N, &K);
	for (int i = 0;i < N;i++)
	{
		scanf("%lf", &Len[i]);
	}

	while (R - L >= 1e-10)
	{
		mid = (R + L) / 2;
		if (P(mid))
			L = mid;
		else
			R = mid;
	}
	
	int m = (int)(R * 100);
	double p = (double)m / 100.0;//保留小数点后两位
	printf("%.2f\n", p);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值