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条绳子,他们的长度分别是Li。如果从他们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留2位小数。(例题)

题解 
    也就是求一个x ,   l1/ x +l2/x +l3/x +.....=K,求最大的x。求的过程中中间值x ,如果>=k也时 ,要求最大的x.
    条件C(x)=可以得到K条长度为x的绳子
    区间l=0,r等于无穷大,二分,判断是否符合c(x) C(x)=(floor(Li/x)的总和大于或等于K

 
输入电缆的个数n,和要分的份数k,求出所有电缆的长度和sum,在区间[0,sum/k]内用二分法求出最大的长度。
 
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<math.h>
using namespace std;
#define st 1e-8//10^-8
double a[];
int k, n;
int f(double x)
{
int cnt = ;
for (int i = ; i < n; i++)
{
cnt += (int)(a[i] / x); //括号不能少
}
return cnt;
}
int main()
{ double sum;
scanf("%d%d", &n, &k);
sum = ;
for (int i = ; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
}
sum = sum / k;//平均值一定会比最终取得长度大
double l = , r = sum, mid;
while (fabs(l - r)>st)//控制精度
{
mid = (l + r) / ;
if (f(mid) >= k)//不断逼近,找到可以满足切割数量下的最大长度
l = mid;
else
r = mid;
}
r=r*;
printf("%.2f\n", floor(r)/);//向下取整
}
// 4 2542
// 8.02
// 7.43
// 4.57
// 5.39
// 0.00

poj1064 Cable master(二分)的更多相关文章

  1. 二分法的应用:POJ1064 Cable master

    /* POJ1064 Cable master 时间限制: 1000MS 内存限制: 10000K 提交总数: 58217 接受: 12146 描述 Wonderland的居民已经决定举办地区性编程比 ...

  2. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

  3. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  4. poj1064 Cable master(二分查找,精度)

    https://vjudge.net/problem/POJ-1064 二分就相当于不停地折半试. C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用. #inclu ...

  5. POJ1064 Cable master 【二分找最大值】

    题目:题目太长了! https://vjudge.net/problem/POJ-1064 题意分析:给了你N根长度为小数形式的棍子,再给出了你需要分的棍子的数量K,但要求你这K根棍子的长度必须是一样 ...

  6. POJ1064 Cable master(二分 浮点误差)

    题目链接:传送门 题目大意: 给出n根长度为1-1e5的电线,想要从中切割出k段等长的部分(不可拼接),问这个k段等长的电线最长可以是多长(保留两位小数向下取整). 思路: 很裸的题意,二分答案即可. ...

  7. POJ 1064 Cable master (二分答案)

    题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...

  8. [POJ] 1064 Cable master (二分查找)

    题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...

  9. POJ 1064 Cable master | 二分+精度

    题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...

随机推荐

  1. [P4782]2-SAT问题

    解题关键:2-sat模板,tarjan解决. #include<iostream> #include<cstring> #include<cstdio> #incl ...

  2. Mat 类的内存管理

    使用 Mat 类,内存管理变得简单,不再像使用 IplImage 那样需要自己申请和释放内存.虽然不了解 Mat 的内存管理机制,也无碍于 Mat 类的使用,但是如果清楚了解 Mat 的内存管理,会更 ...

  3. 数据结构 Job

    问题描述 有 n 项工作在等待队列中等待处理,编号为 1-n. 每个工作有个优先级 p.处理机同一时间只能处理一项工作.处理机决定接下来处理哪一项工作的方式为:从队首取出一项工作 x,若等待队列中没有 ...

  4. 跑实验配环境(tensorflow)

    最近在学习用CNN(卷积神经网络)做图像质量评价,选择的论文是CVPR2014-Convolutional neural networks for no-reference image quality ...

  5. JAVA基础语法动手动脑实践体会

    一.运行EnumTest.java程序. public class EnumTest { public static void main(String[] args) { Size s=Size.SM ...

  6. 在控制台使用MySQL数据库

    本篇内容介绍的是如何在控制台下使用MySQL数据库.首先需要安装MySQL数据库应用程序,然后找到MySql的Command Line Client进入之后你会看到,此处需要正确输入密码,否则会直接退 ...

  7. Python学习第三方库Requests: 让 HTTP 服务人类

    转自官方文档:http://cn.python-requests.org/zh_CN/latest/ 快速上手 http://cn.python-requests.org/zh_CN/latest/u ...

  8. 正经学C#_委托

    以前不会,甚至连想去学都没想.啧啧啧,我是何等朽木啊. 我先不说其中理念,或者原理,咱就先说最简单的用法.怎么去使用委托. 委托 Delegate 使用委托,就要先定义一个委托.定义一个委托就要先声明 ...

  9. timestamp 在curl中变成了Xtamp

    目前的解决方案 将timestemp放在数组最前面. [注意:请求的参数中需要将timestamp这个参数放在数组的最前面,不然在GET方式请求中,会出现浏览器将它变成Xtamp,最终导致签名失败]

  10. Serialization之BinaryFormatter

    前言 BinaryFormatter序列化二进制序列化使用二进制编码来生成精简的序列化,以用于存储或基于套接字的网络流等. 内容 下面通过一个小小的例子来给大家说明什么是BinaryFormatter ...