Cable master POJ
来源:互联网 发布:中国的穆斯林问题 知乎 编辑:程序博客网 时间:2024/06/14 07:46
二分
题目描述:仓库中有N条光缆,要求把它们截成总共K段等长的光缆,求截取的每段光缆的最大长度。
解题思路:比较水的二分,就是精度坑人+读题费劲。注意这个题精度要求特别高,另外输出的时候因考虑到实际因素,保留两位小数的时候是把两位之后的数据截掉,并不是默认的四舍五入,所以用到了floor函数。
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <iomanip>#include <cstdlib>#include <cmath>using namespace std;const int maxn=10000+10;const double esp=1e-9;int m,n;double cable[maxn];void solve(){ sort(cable,cable+m); double r=cable[m-1],l=0.0; int cnt; for(int i=0;i<100;i++) { double mid=(l+r)/2; cnt=0; for(int i=0;i<m;i++) { cnt+=(int)(cable[i]/mid); } if(cnt>=n) l=mid; else r=mid; } printf("%.2lf",floor(l*100)/100);}int main(){ scanf("%d %d",&m,&n); for(int i=0;i<m;i++) { scanf("%lf",&cable[i]); } solve(); return 0;}
一般的二分,二分一百次精度基本上就非常精确了。此题中floor的运用也是长经验了。
阅读全文
0 0
- poj 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- POJ-1064-Cable master
- poj 1064--Cable master
- POJ 1064 Cable master
- POJ 1064 Cable master
- poj 1064 Cable master
- poj 1064 Cable master
- POJ(1064)cable master
- POJ 1064 Cable master
- cable master POJ 1064
- POJ 1064 Cable master
- POJ 1064 Cable Master
- POJ-1064 Cable master
- POJ 1064 Cable master
- 每周一本书之《大数据时代的算法:机器学习、人工智能及其典型实例》
- 08_浮点类型_浮点数误差问题
- View的绘制流程分析之二 -- measure
- 聊一聊CSS中的"死循环"
- AOJ.872 背菜谱
- Cable master POJ
- View的绘制流程分析之三 -- layout
- 计算机网络漫谈之UDP和TCP
- Spanning trees in a secure lock pattern UVALive
- View的绘制流程分析之四 -- draw
- 模板类与类模板、函数模板与模板函数等的区别 (四)
- Android程序编译时报错:duplicate entry: android/support/v4/content/res/TypedArrayUtils.class
- ubuntu下从源码编译比特币(Bitcoin)客户端
- IO流