[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 (二分查找)的更多相关文章
- [POJ] 1064 Cable master (二分查找)
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...
- [ACM] poj 1064 Cable master (二进制搜索)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- POJ 1064 Cable master (二分答案)
题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- POJ 1064 Cable master | 二分+精度
题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...
- POJ 1064 Cable master (二分)
题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...
- 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
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
随机推荐
- Mantis 1.2.19 on Windows Server 2012 r2 datacenter 安装及配置随笔
一.前言 新的小团队需要搭建一个缺陷管理的工具,之前用过bugfree,感觉比较适合,但是 禅道不太适合,放弃之,于是又百度推荐的: .JTrac13.BugNet14.BugOnline15.eTr ...
- python类及其方法
python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
- [JavaScript 随笔] 垃圾回收
在 JavaScript 中,由于垃圾回收是自动进行的,所以人们在编码时可能不太会注意这方面.但事实是,一些 webapp 在使用一段时间后,会出现卡顿的现象,特别是那些单页应用,包括 WebView ...
- NK3C 业务权限控制
资源中,添加了一个类型:权限(橙色显示),现在有4种数据: 域管理员:domainAdmin 组织管理员:orgAdmin 组管理员:groupAdmin 一线员工:phoneAdmin 权限控制可以 ...
- webService访问加密
WebService加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个 soaph ...
- hive查看建表语句
查看hive建表语句:show create table tablename; 查看hive表结构:describe tablename; 简写:desc tablename;
- Mycat配置文件schema.xml参数配置
Mycat原理: Mycat的原理中最重要的一个动词是"拦截",它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析:如分片分析.路由分析.读写分离分析.缓存分析等 ...
- 命令行启动win7系统操作部分功能
control.exe /name microsoft.folderoptions 启动资源管理器的 文件夹属性 选项卡 control.exe /name Microsoft.AddHardware ...
- {Reship}{Matting}Image Matting
======================================== http://www.alphamatting.com/index.html ==================== ...
- CSS布局--浮动与清除
浮动和清除 浮动和清除是页面布局的重要属性.浮动的意思是指将元素从常规的文档流中取出来. 当你浮动一个元素的时候,浮动的元素会被浏览器尽量的往上放,能放多高就放多高,一直到某个元素的边界为止. 浮动元 ...