二分搜索 POJ 1064 Cable master
1 /*
2 题意:n条绳子问切割k条长度相等的最长长度
3 二分搜索:搜索长度,判断能否有k条长度相等的绳子
4 */
5 #include <cstdio>
6 #include <algorithm>
7 #include <cstring>
8 #include <cmath>
9 using namespace std;
10
11 const int MAXN = 1e4 + 10;
12 const int INF = 0x3f3f3f3f;
13 double w[MAXN];
14 int n, k;
15
16 int check(double len) {
17 int ret = 0;
18 for (int i=1; i<=n; ++i) {
19 ret += (int) (w[i] / len);
20 }
21 return ret;
22 }
23
24 int main(void) { //POJ 1064 Cable master
25 //freopen ("POJ_1064.in", "r", stdin);
26
27 while (scanf ("%d%d", &n, &k) == 2) {
28 for (int i=1; i<=n; ++i) {
29 scanf ("%lf", &w[i]);
30 }
31 double l = 0, r = 1e9;
32 for (int i=1; i<=100; ++i) {
33 double mid = (l + r) / 2;
34 if (check (mid) >= k) l = mid;
35 else r = mid;
36 }
37 printf ("%.2f\n", floor (l * 100) / 100);
38 }
39
40 return 0;
41 }
编译人生,运行世界!