poj1064 Cable master(二分)
Cable master 求电缆的最大长度(二分法)
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
The input is ended by line containing two 0's.
Output
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output must contain the single number "0.00" (without quotes).
Sample Input
Sample Output
题解
也就是求一个x , l1/ x +l2/x +l3/x +.....=K,求最大的x。求的过程中中间值x ,如果>=k也时 ,要求最大的x.
条件C(x)=可以得到K条长度为x的绳子
区间l=0,r等于无穷大,二分,判断是否符合c(x) C(x)=(floor(Li/x)的总和大于或等于K
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<math.h>
using namespace std;
#define st 1e-8//10^-8
double a[];
int k, n;
int f(double x)
{
int cnt = ;
for (int i = ; i < n; i++)
{
cnt += (int)(a[i] / x); //括号不能少
}
return cnt;
}
int main()
{ double sum;
scanf("%d%d", &n, &k);
sum = ;
for (int i = ; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
}
sum = sum / k;//平均值一定会比最终取得长度大
double l = , r = sum, mid;
while (fabs(l - r)>st)//控制精度
{
mid = (l + r) / ;
if (f(mid) >= k)//不断逼近,找到可以满足切割数量下的最大长度
l = mid;
else
r = mid;
}
r=r*;
printf("%.2f\n", floor(r)/);//向下取整
}
// 4 2542
// 8.02
// 7.43
// 4.57
// 5.39
// 0.00
poj1064 Cable master(二分)的更多相关文章
- 二分法的应用:POJ1064 Cable master
/* POJ1064 Cable master 时间限制: 1000MS 内存限制: 10000K 提交总数: 58217 接受: 12146 描述 Wonderland的居民已经决定举办地区性编程比 ...
- (poj)1064 Cable master 二分+精度
题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...
- poj1064 Cable master
Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...
- poj1064 Cable master(二分查找,精度)
https://vjudge.net/problem/POJ-1064 二分就相当于不停地折半试. C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用. #inclu ...
- POJ1064 Cable master 【二分找最大值】
题目:题目太长了! https://vjudge.net/problem/POJ-1064 题意分析:给了你N根长度为小数形式的棍子,再给出了你需要分的棍子的数量K,但要求你这K根棍子的长度必须是一样 ...
- POJ1064 Cable master(二分 浮点误差)
题目链接:传送门 题目大意: 给出n根长度为1-1e5的电线,想要从中切割出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 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...
- POJ 1064 Cable master | 二分+精度
题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...
随机推荐
- 第3章 springboot接口返回json 3-1 SpringBoot构造并返回一个json对象
数据的使用主要还是以JSON为主,我们不会去使用XML. 这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller. @Respons ...
- 【总结整理】AMAP学习AMAP.PlaceSearch()
http://lbs.amap.com/api/javascript-api/reference/search#m_AMap.PlaceSearch http://lbs.amap.com/api/j ...
- Centos彻底完全删除已安装软件的办法
1.查询是否安装了软件 rpm -qa | grep -i 软件名 rpm -qa | grep php 2.删除已安装的软件包 根据第一步显示的软件包名,一个个删除 sudo rpm -e -- 包 ...
- php环境引起的"syntax error unexpected $end"
今天在网上淘了一段php和javascript的二级联动下拉菜单的代码.本地运行的时候,一直提示错误 syntax error unexpected $end 本来还以为是括号或者标签没有用好,但是仔 ...
- 杭电acm 1039题
这道题也比较简单,写三个函数判断三个条件即可..... 但是开始时我按照已经注释掉的提交,居然提示WA,我百思不得其解,后改成上面的判断式就可以了,求高手解答.... #include "i ...
- tarjan进阶
一.边双连通分量 定义 若一个无向图中的去掉任意一条边都不会改变此图的连通性,即不存在桥,则称作边双连通图.一个无向图中的每一个极大边双连通子图称作此无向图的边双连通分量. 实际求法和强连通分量差不多 ...
- Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework
Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- bootstrap.js 文件使用指南
介绍 使用 Bootstrap v3.3.7 时,需要引入三个脚本文件. https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.m ...
- CodeForces 404C Restore Graph (构造)
题意:让人构造一个图,满足每个结点边的数目不超过 k,然后给出每个结点到某个结点的最短距离. 析:很容易看出来如果可能的话,树是一定满足条件的,只要从头开始构造这棵树就好,中途超了int...找了好久 ...