Cable master

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 65358   Accepted: 13453

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

题意:在n段电缆中剪出k段一样长的电缆,求能剪出的最长长度

分析:利用用二分法求出符合条件的最大长度(注意控制精度)

#include<iostream>
#include<string.h>
#include<cmath>
#include<algorithm>
#define limit 1e-3//10^-3以下就行
using namespace std;
double a[10001];
bool check(double mid,int n,int k)
{
int cnt=0;
for(int i=0;i<n;i++)
cnt+=(int)(a[i]/mid);//剪出段数的累计和
return cnt>=k;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
double sum=0;
for(int i=0;i<n;i++)
scanf("%lf",&a[i]),
sum=max(sum,a[i]);
double mid,x=0.0,y=sum;
while(y-x>limit)//精度
{
mid=(x+y)/2;
if(check(mid,n,k))
x=mid;//向右取
else y=mid;//向左取
}
printf("%.2f\n", floor(y*100)/100);//floor(y*100)向下取整,舍掉y小数点后三位以下数字
return 0;
}

POJ 1064 Cable master (二分法+精度控制)的更多相关文章

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

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

  2. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  3. poj 1064 Cable master 判断一个解是否可行 浮点数二分

    poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...

  4. 二分搜索 POJ 1064 Cable master

    题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...

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

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

  6. POJ 1064 Cable master (二分查找)

    题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...

  7. POJ 1064 Cable master (二分)

    题目链接: 传送门 Cable master Time Limit: 1000MS     Memory Limit: 65536K 题目描述 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长 ...

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

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  9. POJ 1064 Cable master

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37865   Accepted: 8051 Des ...

随机推荐

  1. backgroud-size属性

    backgroud-size:100% 改为:backgroud-size:100% 100%; 就能解决下面的问题.

  2. linux 分区方案

    背景 之前安装过linux好多次,也学习过好几次,竟然也是一直没开窍,这次不知为啥醒悟了.了解了linux的分区道道.总结起来就是分区主要是根目录(类似c盘),swap,boot(必须有的,  win ...

  3. 024_nginx之backlog坑

    一. 线上碰到一个nginx调优的一个设置,即listen后面设置 listen 80 backlog=1024; 但是多个域名都设置这个值的时候就会出现以下的提示重复报错. 关于backlog参数的 ...

  4. $Django Paginator分页器 批量创建数据

    1批量插入数据: User_list=[]for i in range(100): User_list.append(User(name='小明%s'%i,pwd='abcdefg%s'%i))# 两 ...

  5. js 常用的工具函数

    1 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } i ...

  6. 自定义session,cookie

    第一种情况:没有设置缓存:执行相对应的setitem等方法进行,保存到字典里面 cookies_dic={}print(cookies_dic)class Session(): def __init_ ...

  7. 04 if条件判断 流程控制

    条件判断 if 语法一: if 条件: # 条件成立时执行的子代码块 代码1 代码2 代码3 示例: sex='female' age=18 is_beautiful=True if sex == ' ...

  8. aview安装和使用

    一.安装aalibwget https://sourceforge.net/projects/aa-project/files/latest/download?source=files --no-ch ...

  9. chrome调试工具怎么限制网速

    在做项目的时候,我们测试的时候有时需要限制网速

  10. Windows服务启动进程----Cjwdev.WindowsApi.dll

    windows服务下无法启动外部程序 做一个windows服务监听服务,涉及到windows服务启动外部程序的一个过程,但是调试测试发现,无法简单的用process.start()这种方法, 原因是在 ...