POJ-1064 Cable master---二分答案

题目链接:

https://vjudge.net/problem/POJ-1064

题目大意:

有N条绳子,长度分别是Li,如果从中切割出k条长度相同的绳子,最长多少

解题思路:

二分答案,判断当前解是否可行,由于是浮点数,可以考虑循环100次求值

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn = 100005;
 6 double a[maxn];
 7 int n, k;
 8 bool judge(double x)
 9 {
10     int tot = 0;
11     for(int i = 1; i <= n; i++)
12         tot += (int)(a[i] / x);
13     return tot >= k;
14 }
15 int main()
16 {
17     cin >> n >> k;
18     for(int i = 1; i <= n; i++)cin >> a[i];
19     double l = 0, r = maxn;
20     for(int i = 0; i < 100; i++)
21     {
22         double mid = (l + r) / 2;
23         if(judge(mid))l = mid;
24         else r = mid;
25     }
26     printf("%.2f\n", (double)(int)(r * 100) / 100);
27     return 0;
28 }

 

posted @ 2018-04-29 16:28  _努力努力再努力x  阅读(115)  评论(0编辑  收藏  举报