[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. ...
随机推荐
- HYSBZ 2243 染色 (树链拆分)
主题链接~~> 做题情绪:这题思路好想.调试代码调试了好久.第一次写线段树区间合并. 解题思路: 树链剖分 + 线段树区间合并 线段树的端点记录左右区间的颜色.颜色数目.合并的时候就用区间合并的 ...
- 读书时间《JavaScript高级程序设计》一:基础篇
第一次看了<JavaScript高级程序设计>第二版,那时见到手上的书,第一感觉真是好厚的一本书啊.现在再次回顾一下,看的是<JavaScript高级程序设计>第三版,并记录一 ...
- c++学籍管理系统
程序在编译时出错(vc++ 6.0) 求哪位大神帮忙改改 #include<iostream> #include <string> #include<conio.h> ...
- 小谷的战斗Jquery(三)--水平和垂直菜单
日薪的例子似乎有点低,今天做多.行,这种实现是一个简单的菜单,Web项目中,有两个共同的菜单:纵向和横向.说到从垂直,看原代码. html代码实现最主要的菜单与子菜单 <span style=& ...
- Paypal-Express Checkout快捷支付方式的android端开发心得(二)
一.前导 上一篇讲的不是非常好,这里再又一次讲一下. Paypal手机支付有2种形式: 1.Mobile Express Checkout,MEC,快捷支付 2.MPL 假设採用MEC支付方式,这样的 ...
- 父类中可继承方法在处理private的一个demo
public abstract class AbstractParent { public AbstractParent() { System.out.println("Hello,pare ...
- Java多线程<1>
1.Java多线程的概念: 线(Thread):它指的是一个任务的从开始执行流程到结束. 穿线提供执行任务的机构.供java条款.在一个程序可以启动多个并发线程.候执行. 在单处理器系统中,多个线程共 ...
- jxl创Excel档java示例代码说明
记得下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import j ...
- [Python 学习] 两、在Linux使用平台Python
在本节,它介绍了Linux如何使用平台Python 1. Python安装. 今天,大多数把自己的版本号Python的,它不能被安装.假设你要安装它,可以使用相应的安装指令. Fedora:先以roo ...
- HDU 别easy在一系列的
别easy在一系列的 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Su ...