描述


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. POJ 1050 To the Max -- 动态规划

    题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...

  2. OpenJudge/Poj 2013 Symmetric Order

    1.链接地址: http://bailian.openjudge.cn/practice/2013 http://poj.org/problem?id=2013 2.题目: Symmetric Ord ...

  3. C++对象创建与释放

    创建对象有以下四种形式: #include <iostream> using namespace std; class A{ private: int i; public: A(){ co ...

  4. vmware RHEL6.x 开启FTP和TELNET服务--root权限

    //vmware RHEL6.x默认未安装ftp工具,需自己安装--root权限 第一部分:ftp //检查ftp是否安装 # rpm -qa | grep -i vsftpd //找到ftp的rpm ...

  5. MVC4升级MVC5 异常处理

    使用过程中的一些烦人的事情,权当这篇文章是MVC4升级MVC5的异常合集吧,后期不定期更新. 在这里你可以看到ASP.NET MVC的发展历程以及你需要的版本和目前最稳定的版本.戳ASP.NET MV ...

  6. Statusbar出现透明及界面下方出现空白

    步骤1.在ViewController中 loadView   #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 if ( IOS7_OR_LATER ) ...

  7. CODEVS 2102 石子归并 2

    [题目描述 Descriptin] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法, ...

  8. 实现n皇后问题(回溯法)

    /*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...

  9. vs2013下使用Assist X的破解方法

    Assist X的破解下载:http://pan.baidu.com/s/1kTnDH23 密码:j9jp 01.安装,点击VA_X_Setup2042.exe 安装 02.破解 找到这样的目录:C: ...

  10. 正确配置jstl的maven依赖,jar包冲突的问题终于解决啦

    困扰了两天的问题,非常头疼,今天终于有了解决思路了,说到底,还是对maven不够了解吧.总是抱怨maven不好用,出现各种无厘头的问题,原来这些都是归于对它不够了解不够熟悉,它提供了很好的解决思路,只 ...