POJ_1064_Cable_master_(二分,假定一个解并判断是否可行)
描述
http://poj.org/problem?id=1064
有n条绳子,长度分别为l[i].如果从它们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多少?
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 35271 | Accepted: 7515 |
Description
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
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
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_(二分,假定一个解并判断是否可行)的更多相关文章
- Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)
Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...
- POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)
题意: 有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...
- 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee”
// 有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee” List list = new ArrayList(); list.add("world") ...
- 排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
#include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...
- python 一个二维数组和一个整数,判断数组中是否含有该整数
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. de ...
- POJ-3104 Drying---二分答案判断是否可行
题目链接: https://cn.vjudge.net/problem/POJ-3104 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服每 ...
- Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
Jquery 选择器 详解 在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...
随机推荐
- About 'atoi'
在很多地方我们看到的都是:atoi 是一个 C Reference function atoi() convert a string to an integer. This function of s ...
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- grant授权“失败”的原因
在创建用户的时候我们通常采用grant命令完成,并同时赋予相应的权限,例如我们创建一个名为test的用户,g并赋予其对数据库foo下所有表格select,delete,drop,create权限: g ...
- Weui 微信网站开发样式插件使用教程
微信的网页样式正式发布了,搜了一下,正式引入了乐学一百微信端的项目中. <div class="weui_grids"> <a href="javasc ...
- linux系统制作简单流程
制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...
- winFrom窗体样式
ControlBox窗口样式:确定窗体是否有"控件/系统"菜单框. 设置为隐藏 False AutoSizeMode GrowAndShrink 指定用户界面元素自动调整自身大小 ...
- 开发设计模式(三)策略模式(Strategy Pattern)
转自http://blog.sina.com.cn/s/blog_89d90b7c01017zrr.html 下面的环境是unity3d,用C#进行编码,当然有人会说这是在乱用模式,U3D不一定适合使 ...
- theano log softmax 4D
def softmax_4d(x_4d): """ x_4d: a 4D tensor:(batch_size,channels, height, width) &quo ...
- C++学习笔记37:元编程
元编程 什么是元编程(metaprogramming) 利用模板可以进行编译期计算(数值计算,型式计算和代码计算)的特点进行程序设计 为什么可以进行元编程? C++是两层语言:执行编译期计算的代码称为 ...
- Xcode - 必不可少的插件
1. backLight 2. BBUDebuggerTuckAway 3. Cocoapod 4. FuzzyAutoComplete 5. HOStringSense 6. KissImageN ...