描述


http://poj.org/problem?id=1064

有n条绳子,长度分别为l[i].如果从它们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多少?

Cable master
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35271   Accepted: 7515

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

Source

分析


二分.

条件C(x)=可以得到k条长度为x的绳子,求满足C(x)的x的最小值.求解这样的最大化或最小化问题:"假定一个解并判断是否可行".(普通二分查找值的条件C(x)=v).

注意:

1.关于精度,这个貌似不太好估算,所以就循环100次咯.

 #include<cstdio>
#include<algorithm>
using std :: max; const int maxn=;
int n,k;
double maxl,l[maxn]; bool judge(double x)
{
int sum=;
for(int i=;i<=n;i++)
{
sum+=(int)(l[i]/x);
}
return sum>=k;
} double bsearch(double x,double y)
{
for(int i=;i<;i++)
{
double m=x+(y-x)/;
if(judge(m)) x=m;
else y=m;
}
return x;
} void init()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%lf",&l[i]);
maxl=max(maxl,l[i]);
}
} void solve()
{
printf("%.2f\n",floor(bsearch(,maxl)*)/);
} int main()
{
freopen("Cable.in","r",stdin);
freopen("Cable.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}

POJ_1064_Cable_master_(二分,假定一个解并判断是否可行)的更多相关文章

  1. Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)

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

  2. POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)

    题意:       有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...

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

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

  4. 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...

  5. 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee”

    // 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee” List list = new ArrayList(); list.add("world") ...

  6. 排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。

    #include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...

  7. python 一个二维数组和一个整数,判断数组中是否含有该整数

    在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. de ...

  8. POJ-3104 Drying---二分答案判断是否可行

    题目链接: https://cn.vjudge.net/problem/POJ-3104 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服每 ...

  9. Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串

    Jquery 选择器 详解   在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...

随机推荐

  1. OpenJudge 2773 2726 2727 采药

    1.链接地址: http://bailian.openjudge.cn/practice/2773/ http://bailian.openjudge.cn/practice/2726/ http:/ ...

  2. 【原创】开机出现grub rescue,修复办法

    出现这种问题 一般在于进行了磁盘分区(GHOST备份时也会造成)导致grub引导文件找不到.我们只要让它找到引导文件就好了. 此时屏幕上提示grub resume>  我们先输入set看下现在g ...

  3. css中>,+,~的用法

    1.>的用法 A>B选择A的所有子元素B(只选择一代) 2.+的用法 A+B选择紧随着A的兄弟元素B 3.~的用法 A~B选择位置处于A后面的所有兄弟元素B(不包含兄弟元素的子元素)

  4. php四种基础排序算法的运行时间比较

    /** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...

  5. ora_reco_070361 hs message to agent event 等待事件

    现象描述:oracle通过透明网关查询Teradata数据库的表突然变得很慢,小表可以查询出来,大表干脆就出不来. 分析过程: 1.网络可以ping通,小表可以过来,说明网络和网关都没有什么问题的. ...

  6. 2014年辛星完全解读Javascript第一节

    ***************概述*************** 1.Javascript是一种原型化继承的基于对象的动态类型的脚本语言,它区分大小写,主要运行在客户端,用户即使响应用户的操作并进行数 ...

  7. 【springmvc Request】 springmvc请求接收参数的几种方法

    通过@PathVariabl注解获取路径中传递参数 转载请注明出处:springmvc请求接收参数的几种方法 代码下载地址:http://www.zuida@ima@com/share/1751862 ...

  8. Android初步 简单demo

    刚入门不久,没学JAVA,从C++转过来的,C++的QT和安卓简直有异曲同工之妙,为了加深自己对安卓的理解,特写博客以记录,望大神们多多指点. 效果图,刚入门的话,肯定要熟悉基本的控件的使用,这跟我学 ...

  9. C# dynamic

    [TestMethod] public void DynamicTest() { dynamic Customer = new ExpandoObject(); Customer.Name = &qu ...

  10. SpringMVC的controller方法中注解方式传List参数使用@RequestBody

    在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...