Cable master
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49944   Accepted: 10493

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条绳子,他们的长度分别为Li。如果从他们中切割出k条长度相同的绳子的话,这k条绳子最长能有多长?答案保留小数点后两位。

思路:这个问题采用二分法很容易得到答案。
二分法模板:
int n,k;
const int maxn=1000;
int a[maxn];
void solve()
{
int lb=-1,ub=n;
while(ub-lb>1)
{
int mid=(lb+ub)/2;
if(a[mid]>=k)
ub=mid;
else
lb=mid;
}
printf("%d",ub);
}

题目ac代码:

#include <iostream>
#include<math.h>
#include<stdio.h>
using namespace std; const int maxn=10005;
double l[maxn];
int n,k;
const double inf=99999999; bool check(double x)
{
int sum=0;
for(int i=0;i<n;i++)
sum+=(int)(l[i]/x);
return sum>=k;
}
void solve()
{
double lb=0,ub=inf;
//重复循环,直到解的范围足够小
for(int i=0;i<100;i++)
{
double mid=(ub+lb)/2;
if(check(mid))
lb=mid;
else ub=mid;
}
printf("%.2f\n",floor(ub*100)/100);
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
{
scanf("%lf",&l[i]);
}
solve();
return 0;
}




















												

二分搜索poj106的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)

    题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k  分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...

  5. hdu 1075 二分搜索

    还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...

  6. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  7. HDU 2852 KiKi's K-Number(树状数组+二分搜索)

    题意:给出三种操作 0 e:将e放入容器中 1 e:将e从容器中删除,若不存在,则输出No Elment! 2 a k:搜索容器中比a大的第k个数,若不存在,则输出Not Find! 思路:树状数组+ ...

  8. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  9. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

随机推荐

  1. 移动端click事件延迟300ms该如何解决

    window.addEventListener( "load", function() {     FastClick.attach( document.body ); }, fa ...

  2. 博弈论入门题 kiki's game

    Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his mind ...

  3. 创建Django项目(六)——模板

    2013-08-07 22:42:30|           1.设置模板路径         打开 settings.py 文件,修改 TEMPLATE_DIRS 内容,指向模板存放的绝对路径,而不 ...

  4. request详究

    本文主要是对在学习过程中遇到的request用法进行归纳总结,彻底的搞明白request在jsp中的作用. 百度百科的介绍如下: Request对象的作用是与客户端交互,收集客户端的Form.Cook ...

  5. Kafka单机Windows环境搭建

    Kafka单机Windows环境搭建 1,安装jdk1.8:安装目录不能有中文空格: 2,下载zookeeper,https://mirrors.cnnic.cn/apache/zookeeper/z ...

  6. 【前端】JavaScript继承实现的四种方式

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/4770235.html 一.继承的实现方法 1.原型链继承 这个继承最为简单,它的实现原理是,每一个AO对象都有一 ...

  7. powershell 的版本号所引起的载入 FSharp 编译器问题

    powershell 的版本号所引起的载入 FSharp 编译器问题 在 64 位的系统下,大部分系统文件都有 64 位和 32 位的版本号:通常在C:\WINDOWS\system32 下的是 64 ...

  8. ArcGIS中生成蜂窝多边形算法解析

    近来有不少同学.都有问我关于蜂窝多边形的问题.也就是正六边形,也就是以下这个东东: 一般的问答模式例如以下: 亲们问:ArcGIS里面那个工具能够做这个东东? 虾神答:额,没有原生的工具. 亲们问:那 ...

  9. JavaScript基础 -- js常用内置方法和对象

    JS中常用的内置函数如下: 1.eval(str):计算表达式的结果. 2.parseInt(str,n):将符串转换成整数数字形式(可指定几进制). 3.parseFloat(str):将字符串转换 ...

  10. ip(点分十进制 <==> 二进制整数)之间的转换

    linux的套接字部分比较容易混乱,在这里稍微总结一下. 地址转换函数在地址的文本表达式和它们存放在套接字地址结构中的二进制值进行转换. 地址转换函数有四个:其中inet_addr 和 inet_nt ...