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(x)成立的最大的x。

提示:为了避免实数运算误差,把电缆长度放大100倍,化为整数,最后不要忘了把结果再除以100即可。

 1 const
 2   maxn=10000;
 3 var
 4   a:array[1..maxn] of longint;
 5   n,k:longint;
 6   procedure init;
 7     var i:longint; x:real;
 8     begin
 9       assign(input,‘poj1064.in‘); reset(input);
10       readln(n,k);
11       for i:=1 to n do
12         begin
13           readln(x);
14           a[i]:=trunc(x*100);
15         end;
16       close(input);
17     end;
18   function can(x:longint):boolean;
19     var s,i:longint;
20     begin
21       s:=0;
22       for i:=1 to n do
23         s:=s+a[i] div x;
24       exit(s>=k);
25     end;
26   function find:longint;
27     var l,r,mid:longint;
28     begin
29       l:=0; r:=10000000;
30       while l<r do
31         begin
32           mid:=(l+r+1) div 2;
33           if can(mid) then l:=mid   //还可能再加长
34           else r:=mid-1; // 过长无法切割k份,需缩短
35         end;
36       exit(l);
37     end;
38 begin
39   init;
40   writeln(find/100.0:0:2);
41 end.

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

时间: 2024-08-09 14:48:54

poj1064 cable master(最大值问题:二分+贪心)的相关文章

Cable master HDU - 1551 —— 二分基础模板(精度问题)

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 connect computers for the contestants using a "star&

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

poj 1064 Cable master【浮点型二分查找】

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29554   Accepted: 6247 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" 题解: 满足单调性,来二分吧. 心得: 来,我们看着代码说话. 判无解的处理 首先最多能

POJ 1064 Cable master(初遇二分)

题目链接:http://poj.org/problem?id=1064 题意:有n条绳子,他们的长度是Li,如果从他们中切割出K条长度相同的绳子,这相同的绳子每条有多长,输出至小数点后两位 " then the output file must contain the single number "0.00" (without quotes)."不是四舍五入到两位,一般四舍五入题目会说"bounded to"的提示 显然想得到的绳子越短就越能得到

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

【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