【判断解是否可行-二分】POJ1064-Cable master

http://poj.org/problem?id=1064

【题目大意】

给出几条绳子的长度,问如果要切出k条长度相等的绳子,这k条绳子最常多长?

【思路】

二分。把下界设为0,上界设为所有绳子长度总和,每次取mid,判断如果每条绳子长为mid切出条数是否≥k,满足则将下边界设为mid,否则把上边界设为mid。因为是要保留小数的,所以通过循环来进行精确。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 using namespace std;
 7 const int MAXN=10000+500;
 8 int n,k;
 9 double len[MAXN];
10 double sum;
11
12 int whether(double x)
13 {
14     int num=0;
15     for (int i=0;i<n;i++)
16     {
17         num+=(int)(len[i]/x);
18     }
19     return num>=k;
20 }
21
22 int main()
23 {
24     scanf("%d%d",&n,&k);
25     for (int i=0;i<n;i++)
26     {
27         scanf("%lf",&len[i]);
28         sum+=len[i];
29     }
30     double lb=0,ub=sum;
31     for (int i=0;i<100;i++)
32     {
33         double mid=(lb+ub)/2;
34         if (whether(mid)) lb=mid;
35         else ub=mid;
36     }
37     printf("%.2lf\n",floor(ub*100)/100);
38     return 0;
39 }
时间: 2024-10-06 10:40:19

【判断解是否可行-二分】POJ1064-Cable master的相关文章

poj1064 cable master(最大值问题:二分+贪心)

题意: 有n条电缆,他们的长度分别为l[i].如果从n条电缆中切割出K条长度相同的电缆的话,这k条电缆每条最长能多长?答案小数点后保留两位有效数字. 输入: n, k n行:l[i] Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 数据范围: 1<=N<=10000; 1<=k<=10000; 1<=l[i]<=100000. 分析: 设命题:can(x)=能切割出k条长度为x的电缆. 问题转化:求can

POJ1064 Cable master 【精度问题】

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

poj1064 Cable master

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

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;

【自用】POJ1064 Cable master 且来说说卡精度的心得

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44347241"); } 题意: 多组数据,n个小棒,分成m段,最长多长? 不能短于0.01,如果分不出来,输出"0.00" 题解: 满足单调性,来二分吧. 心得: 来,我们看着代码说话. 判无解的处理 首先最多能

POJ1064——二分——Cable master

Language: Default Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29971   Accepted: 6349 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered an

[ACM] poj 1064 Cable master (二分查找)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 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(二分查找+精度)(神坑题)

POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条件写错了,wa了n次,当 c(mid)<=k时,令ub=mid,这个判断是错的,因为要找到最大切割长度,当满足这个条件时,可能已经不是最大长度了,此时还继续缩小区间,自然就wa了,(从大到小递减,第一次满足这个条件的值,就是最大的值),正确的判断是当 c(mid)<k时,令ub=mid,这样循环1

【POJ - 1064】Cable master(二分)

Cable master 直接上中文了 Descriptions 输入2个数 N  K n条绳子    要分成大于等于k段 求每段最长多长呢?并且每段不能小于1cm 必须以厘米精度写入数字,小数点后正好是两位数.如果无法切割所请求的每个长度至少为1厘米的件数,则输出文件必须包含单个数字“0.00”(不带引号). 多组文件输入 Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 题目链接 https://vjudge.net/probl