[ACM] poj 1064 Cable master (二进制搜索)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21071 | Accepted: 4542 |
Description
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
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
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
解题思路:
N条绳子,长度为别为li,要截成长度相等的K段,问切成小段的最大长度是多少。有的绳子能够不切。
也就是求一个x , l1/ x +l2/x +l3/x +.....=K,求最大的x。
求的过程中中间值x 。假设>k也是符合题意的。要求最大的x。==k.
条件C(x)=能够得到K条长度为x的绳子
区间l=0,r等于无穷大,二分。推断是否符合c(x) C(x)=(floor(Li/x)的总和大于或等于K
代码:
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using namespace std;
const int maxn=10003;
const int inf=0x7fffffff;
double l[maxn];
int n,k; bool ok(double x)//推断x是否可行
{
int num=0;
for(int i=0;i<n;i++)
{
num+=(int)(l[i]/x);
}
return num>=k;//被分成的段数大于等于K才可行
} int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
scanf("%lf",&l[i]);
double l=0,r=inf;
for(int i=0;i<100;i++)//二分,直到解的范围足够小
{
double mid=(l+r)/2;
if(ok(mid))
l=mid;
else
r=mid;
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<floor(l*100)/100;//l和r最后相等
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[ACM] poj 1064 Cable master (二进制搜索)的更多相关文章
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- 二分搜索 POJ 1064 Cable master
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- POJ 1064 Cable master (二分)
题目链接: 传送门 Cable master Time Limit: 1000MS Memory Limit: 65536K 题目描述 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长 ...
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
- poj 1064 Cable master【浮点型二分查找】
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29554 Accepted: 6247 Des ...
- POJ 1064 Cable master (二分法+精度控制)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65358 Accepted: 13453 De ...
- POJ 1064 Cable master (二分查找)
题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...
随机推荐
- Android - 数据存储 -存储键值对
如果你有少量的键值数据需要存储,可以使用SharedPreferencesAPI.SharedPreferences对象指向一个包含键值对的文件并且提供了一些简单的方法来读取它们.每个SharedPr ...
- Extjs Web Desktop申请书
今天我Web Desktop应用基本完成.多语言支持.现有asp,php,jsp版本号. 废话拍了几张照片让大家有一个直观的了解: watermark/2/text/aHR0cDovL2Jsb2cuY ...
- js的StringBuffer实施和使用类
<strong>JAVA有一个StringBuffer分类,js但不是在下面,以实现自己的简单js的StringBuffer分类.</strong> //创建一个StringB ...
- MCC460MNC08
因为搜得辛苦,正好也写点关于我morto工作有关的事情,给大家和我一样扫盲,哈哈 The GSM Mobile Country Code (MCC) is different from the i ...
- bnu 34982 Beautiful Garden(暴力)
题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和 ...
- 每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询
前言: 前一段时间一直都比較忙.没有什么时间进行总结,刚好节前项目上线.同一时候趁着放假能够好好的对之前遇到的一些问题进行总结. 主要内容有使用SharePoint服务端对象模型进行查询.为Share ...
- 如何成为游戏的生产者——第二章:如何开始你的编程(开发环境的搭建、C++语言适应)
如何成为游戏的生产者--文章二章:怎样開始你的编程 小故事:上节说到我六年级打开了那本C语言的书,然后其实我还是没看懂.好像看懂了一些printf语句.之后遇到了史无前例的困难--怎么让代码执行起来. ...
- 看你的门-攻击服务器(4)-HTTP参数注入攻击
首先需要声明.这纯粹是没有远见和有点真才实学开发一个愚蠢的观点,只为web参考系统安全. 1.HTTP参数注入攻击 參数,被用做后端HTTP请求中的參数,这个时候就有可能会导致HTTP參数注入. 一个 ...
- div显示和隐藏
它是实现比较简单.style.display控制层隐藏或显示属性. <html> <body> <script> function show(){ document ...
- Finding awesome developers in programming interviews(转)
英文原文:Finding awesome developers in programming interviews 我曾在一次面试中要求一个很有经验的嵌入式软件开发人员写出一个反转一段字符串并输出到屏 ...