首页 > 其他 > 详细

Divide and conquer:Cable Master(POJ 1064)

时间:2015-12-22 06:25:01      阅读:194      评论:0      收藏:0      [点我收藏+]

                技术分享

                 缆绳大师

  题目大意,把若干线段分成K份,求最大能分多长

  二分法模型,C(x)就是题干的意思,在while那里做下文章就可以了,因为这个题目没有要求长度是整数,所以我们要不断二分才行,一般50-100次就可以了,精度足够了

  最后要注意,这一题有四舍五入的陷阱,最好用floor处理一下

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 #include <math.h>
 5 #define MAX 1000000
 6 
 7 using namespace std;
 8 
 9 static double s_set[10010];
10 
11 bool C(const double, const int, const int);
12 
13 int main(void)
14 {
15     int string_sum, cnt;
16     double lb, rb, mid;
17 
18     while (~scanf("%d%d", &string_sum, &cnt))
19     {
20         for (int i = 0; i < string_sum; i++)
21             scanf("%lf", &s_set[i]);
22         lb = 0; rb = MAX;
23 
24         for (int i = 0; i < 50; i++)//50的精度已经足够了
25         {
26             mid = (lb + rb) / 2;
27             if (C(mid, string_sum, cnt))
28                 lb = mid;
29             else
30                 rb = mid;
31         }
32         printf("%.2f\n", floor(mid * 100) / 100);//防止四舍五入
33     }
34 
35     return 0;
36 }
37 
38 bool C(const double cut, const int string_sum, const int K)
39 {
40     int tmp_sum = 0;
41     for (int i = 0; i < string_sum; i++)
42         tmp_sum += (int)(s_set[i] / cut);
43 
44     return tmp_sum >= K;
45 }

技术分享

Divide and conquer:Cable Master(POJ 1064)

原文:http://www.cnblogs.com/Philip-Tell-Truth/p/5065417.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!