HDU 1551 Cable master【二分答案】

题意:给出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 }

时间: 2024-10-13 07:14:08

HDU 1551 Cable master【二分答案】的相关文章

hdu 1551 Cable master(二分)

Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2003    Accepted Submission(s): 751 Problem Description Inhabitants of the Wonderland have decided to hold a regional programming co

POJ 1064 Cable master (二分答案)

题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉得关于浮点数的二分for循环比while循环更好一点.注意最后要用到floor 保证最后答案不会四舍五入. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std;

POJ 1064 Cable master (二分 分数化整数)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28764   Accepted: 6091 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

(poj)1064 Cable master 二分+精度

题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to conn

HDU - 5884 Sort (二分答案+贪心)

有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 2 3 4 5 Sample Output 3 这个题很容易想到二分答案+优先队列check 然而这样复杂度是 O(n logn*logn ),会TLE(这特么都会TLE?加个读入优化就过了) 可以先给所有数字排个序,然后用两个队列,一个存原来的数字,一个存新合成的数字. 所以两个队列都是有序的.

Cable master(二分)

Cable master Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30     Accepted Submission(s): 15 Special Judge Description Inhabitants of the Wonderland have decided to hold a regional programmin

POJ 1064 Cable master (二分)

题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include &l

POJ1064 Cable master(二分)

本题用二分搜索能够非常easy的求出答案.设条件C(X)为能够得到K条长度为X的绳子,C(x)=(floor(L(i)/x)).X的初始范围为(0,Max(L(i))+1). #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; double a[10005]; int n,k;

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

题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可.复杂度o(nlogm) 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include