Cable master 题解
来源:互联网 发布:淘宝旺旺号名字大全 编辑:程序博客网 时间:2024/06/10 03:40
浮点数的二分写的我难受,因此分享给大家一道题目
HDU1551:Cable master(二分)
题意:给出n条线段,以米的单位给出,小数点后两位(精确到厘米),要你对这些线段裁剪,裁剪出m条等长的线段,并且让这些线段尽可能长另外线段的长度不能小于1厘米,如果筹不够m条,输出0.00
下面是浮点数代码写法
#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>#define eps 1e-8using namespace std;double a[10005];int n,k;bool ok(double mid){ int count=0; for(int i=0; i<n; i++) { count+=(int)(a[i]/mid); } if(count>=k) return 1; else return 0;}int main(){ while(~scanf("%d%d",&n,&k)) { double sum=0.0; for(int i=0; i<n; i++) { scanf("%lf",&a[i]); sum=max(sum,a[i]); } double l=0,r=sum; for(int i=0;i<100;i++) { double mid=(l+r)/2.0; if (ok(mid)) l=mid; else r=mid; } int tmp = r * 100;//由于精确到两位小数 printf("%.2f\n",tmp*0.01); } return 0;}
接下来还有一种整数写法
#include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>#define eps 1e-8using namespace std;int b[10005];int n,k;bool ok(int mid){ int count=0; for(int i=0; i<n; i++) { count+=b[i]/mid; } if(count>=k) return 1; else return 0;}int main(){ while(~scanf("%d%d",&n,&k)) { int sum=0; double len; for(int i=0; i<n; i++) { scanf("%lf",&len); b[i]=len*100; sum=max(sum,b[i]); } int l=0,r=sum; while(l<=r) { int mid=(l+r)>>1;//特别注意的是要对0特判,不然会runtime error if(mid==0) { r=0; break; } if (ok(mid)) l=mid+1; else r=mid-1; } printf("%.2f\n",(double)r/100); } return 0;}
阅读全文
0 0
- Cable master 题解
- poj1064:Cable master——题解
- Cable master
- Cable Master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- Cable master
- cable master
- pku1064 Cable master
- 【Scikit-Learn 中文文档】线性和二次判别分析
- Moving Pegs UVA
- 89. Gray Code
- 获取DOM元素尺寸及鼠标位置
- 震惊!从未见过如此简单粗暴的Hibernate教程
- Cable master 题解
- 【Scikit-Learn 中文文档】内核岭回归
- 陈国君java第五版第四章课后习题第五题
- JAVA如何使用Redis
- 详解HashMap和HashTable
- 9BRunning Student
- js调用百度GPS定位
- 使用Java爬虫当数据后台的资源网站搭建
- HTTP协议