题目链接:http://poj.org/problem?id=1064

有n条绳子,长度分别是Li。问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少。

二分答案,尤其注意精度问题。我觉得关于浮点数的二分for循环比while循环更好一点。注意最后要用到floor 保证最后答案不会四舍五入。

 #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n , m;
double len[int(1e4 + )]; bool check(double x) {
int res = ;
for(int i = ; i < n ; ++i)
res += int(len[i] / x);
return res >= m;
} int main()
{
while(~scanf("%d %d" , &n , &m)) {
for(int i = ; i < n ; ++i)
scanf("%lf" , len + i);
double l = , r = 10000000.0;
for(int i = ; i < ; ++i) {
double mid = (l + r) / 2.0;
if(check(mid))
l = mid;
else
r = mid;
}
printf("%.2f\n" , floor(l * ) / );
}
}

POJ 1064 Cable master (二分答案)的更多相关文章

  1. [POJ] 1064 Cable master (二分查找)

    题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...

  2. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

  3. POJ 1064 Cable master | 二分+精度

    题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...

  4. POJ 1064 Cable master (二分)

    题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...

  5. poj 1064 Cable master 判断一个解是否可行 浮点数二分

    poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...

  6. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  7. 二分搜索 POJ 1064 Cable master

    题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...

  8. POJ 1064 Cable master(二分答案)

    嗯... 题目链接:http://poj.org/problem?id=1064 其实这是一道很好想的二分答案的一道题... 二分的区间就是1~max_l,从1开始是因为所有小于1的都需要按0计算,没 ...

  9. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

随机推荐

  1. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  2. HDU 3749 Financial Crisis 经济危机(点双连通分量)

    题意: 给一个图n个点m条边(不一定连通),接下来又q个询问,询问两个点是为“不相连”,“仅有一条路径可达”,“有两条及以上的不同路径可达”三种情况中的哪一种.注:两条以上的路径指的是路径上的点连1个 ...

  3. matlab中,计算,记录,程序运行,起始,结束 时间,间隔 &matlab中 tic,toc函数的用法

    Tic和toc函数可以计算运行一段时间的代码. 例如: clc tic d=zeros(1,10000); for i=1:10000 d(i)=i; end toc tic c=1; for i=1 ...

  4. python - 沙盒环境 - virtualenv - 简明使用录

    1. 不讲安装,没意思 2. 使用 virtualenv ENV # 建立环境,ENV你可以随便定,看起来像是 mkdir ENV cd ENV # 进目录呗 source bin/activate ...

  5. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  6. Java [Leetcode 326]Power of Three

    题目描述: Given an integer, write a function to determine if it is a power of three. Follow up:Could you ...

  7. java运用FFMPEG视频转码技术

    基于windows系统安装FFMPEG转码技术 http://wenku.baidu.com/link?url=z4Tv3CUXxxzLpa5QPI-FmfFtrIQeiCYNq6Uhe6QCHkU- ...

  8. ECSHOP v2.5数据库字典

    ECSHOP v2.5 数据库字典 ECSHOP R&D Team 2007年4月16日 商品相关表 商品分类表 category 此表用来维护商品分类信息 字段名 字段描述 字段类型 默认值 ...

  9. POJ 3233 Matrix Power Serie

    题意:给一个n×n的矩阵A,求S = A + A2 + A3 + … + Ak. 解法:从式子中可得递推式S(n) = S(n - 1) + An,An = An-1×A,可得矩阵递推式 [S(n), ...

  10. [Everyday Mathematics]20150121

    设 $f\in C[0,1]$ 适合 $$\bex xf(y)+yf(x)\leq 1,\quad\forall\ x,y\in [0,1]. \eex$$ 试证: $$\bex \int_0^1 f ...