码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1551 Cable master【二分答案】

时间:2015-05-31 10:51:35      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出n块木板,它们分别的高度,现在要把它们裁切成k块,问裁切成的最大的高度

二分答案,上限是这n块木板里面的最大值 然后每一个答案去判断一下是否满足能够裁切成k块

技术分享
 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 
18 double a[maxn];
19 int n,k;
20 
21 int ok(double x){
22     int ans=0;
23     for(int i=1;i<=n;i++){
24         ans+=(int)(a[i]/x);
25     }
26     if(ans<k) return 0;
27     return 1;
28 }
29 
30 int main(){
31     while(scanf("%d %d",&n,&k)!=EOF&&n&&k){
32         double hmax=-1;
33         for(int i=1;i<=n;i++) scanf("%lf",&a[i]),hmax=max(hmax,a[i]);
34         
35         double l=0.000,r=hmax;
36         
37         while(r-l > 1e-6){
38             double mid=(l+r)/2;
39             if(ok(mid)) l=mid;
40             else r=mid;
41         }
42         printf("%.2lf\n",l);    
43     }
44     return 0;
45 }
View Code

 

HDU 1551 Cable master【二分答案】

标签:

原文地址:http://www.cnblogs.com/wuyuewoniu/p/4541503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!