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. 字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍

    1.ZIP. GZIP  计算机文件压缩算法,JDK中java.util.zip.*中实现.主要包括ZipInputStream/ ZipOutputStream.GZipInputStream/Zi ...

  2. 关于service和线程的区别

    主要有两方面,访问控制和功能区别 首先,service是运行在主线程上的,并不是一个新的线程 其次,service在运行的时候可以被多个activity访问和控制,而线程是不可以的 最后,servic ...

  3. [poj2318]TOYS(直线与点的位置关系)

    解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. 实际就是用右手定则判断的. #i ...

  4. koa的跨域访问

    koa跨域访问:1.安装插件 npm install koa-cors --save-dev2.项目的app.js中var cors = require('koa-cors'); app.use(co ...

  5. SpringMVC_04 拦截器 【拦截器的编程步骤】【session复习?】

    待更新... 2017年5月13日22:45:31 1 什么是拦截器  spring提供的一个特殊组件,前端控制器 DispacherServlet 在收到请求之后,会先调用拦截器,再调用处理器(Co ...

  6. SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数

    数据库中的函数和c#中的函数很相似 按顺序来, 这里价格特别的 print  可以再消息栏里打印东西 数学函数 ceiling()  取上限   不在乎小数点后面有多大,直接忽略 floor()   ...

  7. ruby 类创建-继承-消息

    ############################################# #create ruby a class #@符号表示实例变量,相当于java的private 属性 ### ...

  8. JavaPersistenceWithHibernate第二版笔记-第七章-002Mapping an identifier bag(@CollectionId、@ElementCollection、@CollectionTable、@Type)

    一.结构 A bag is an unordered collection that allows duplicate elements, like the java.util.Collection ...

  9. 数据结构 merge_link合并链表

    问题描述 本题任务是维护一条非递减的链表,初始长度为 0,记这条链表为主链表.对主链表做 N 次操作,操作分两种:1 k a1 a2 … ak,表示一条长度为 k 且非递减的链表,需要将这条链表合并到 ...

  10. 《Effective Java》第10章 发并

    第66条:同步访问共享的可变数据 Java语言规范保证读或者写一个变量是原子的(atomic ) ,除非这个变量的类型为long或者double. [java中long和double类型操作的非原子性 ...