POJ 1064 Cable master (二分查找)
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
分析:
根据最长的棍子二分枚举切割长度。
这点很容易想到。
本题麻烦的地方在于小数的二分。
由于精度丢失问题,如果直接使用double进行二分,那么二分确定的最大长度必然是不精确的。
如:只有1根100.0的棍子,分成10段。如果使用double二分,那么就算把精度控制到0.0000001,
最后的答案也就9.9999666。,而不是10.
由于只要求2位小数,则把所有棍子长度*100,变成整数二分即可。
最后答案ans/=100。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int N,K;
double L[10009];
///判断每小段的长度为x的话,能不能满足分成K段
bool C(double x)
{
int num=0;
for(int i=0; i<N; i++)
{
num+=(int)(L[i]/x);
}
return num>=K;
}
void solve()
{
double lb=0,ub=0x3f3f3f3f;
for(int i=0; i<100; i++)///尽可能的将分的范围具体
{
double mid=(lb+ub)/2;
if(C(mid)) lb=mid;
else ub=mid;
}
printf("%.2lf\n",floor(ub*100)/100);
}
int main()
{
scanf("%d%d",&N,&K);
for(int i=0; i<N; i++)
scanf("%lf",&L[i]);
solve();
return 0;
}
POJ 1064 Cable master (二分查找)的更多相关文章
- [POJ] 1064 Cable master (二分查找)
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...
- 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 ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
随机推荐
- <Effective C++>读书摘要--Templates and Generic Programming<一>
1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...
- intellij idea 如何将一个普通项目转换为maven项目
1.工程文件下新建文件pom.xml,并填写好内容. 2.在pom.xml 文件上右键 Add as Maven Project.
- 制作QQ微信支付宝三合一收款码
转载:http://blog.mambaxin.com/article/56 发现很多博客都带了打赏功能,虽说打赏的人可能很少,但始终是一份心意,能让博主知道自己写的文章有用,能够帮助到人.所以,我也 ...
- [Redis]在.NET平台下的具体应用
一.安装第三方驱动 PM> Install-Package ServiceStack.Redis 二.使用C#语言调用类库访问Redis
- 枚举当前环境中打开的所有IE
IE程序是属于Shell的一个应用程序,要枚举当前打开的所有IE程序窗口,可以通过ShellWindows集合来打开属于Shell的当前的窗口的集合. 首先添加程序需要的头文件和tlb库 //#imp ...
- java.awt.AWTError: Can't connect to X11 window server using ':20' as the value of the DISPLAY variable
1.使用pio在Linux服务器上创建window文件时,需要使用到Linux的图形界面服务,出现以下问题需确认用户权限. 参考文献:https://zhidao.baidu.com/question ...
- Bootstrap 基本模板理解
<!-- 声明文档类型 为 html5 --> <!DOCTYPE html> <!-- 声明页面内容主要为 中文简体 --> <html lang=&quo ...
- BZOJ 1786 配对(DP)
如果我们直接令dp[i][j]为前i个位置第i个位置填j所产生的逆序对的最少数.这样是不满足无后效性的. 但是如果发现对于两个-1,如果前面的-1填的数要大于后面的-1填的数.容易证明把他们两交换结果 ...
- linux 服务器丢包故障排查
项目开了个P2P服务器,但是运行一段时间就会出现丢包问题,具体表现为:1.udp丢包严重(一分钟收发分别1.5W) 2.ssh(用于运维指令)连接不上该服务器(超时) 3.服务器运行好像没什么异常,u ...
- hdu 1690 Bus System (最短路径)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...