zoukankan      html  css  js  c++  java
  • Divide and Conquer:Cable Master(POJ 1064)

                    

                     缆绳大师

      题目大意,把若干线段分成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
    ", 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 }

  • 相关阅读:
    Oracle SQL语句收集
    SqlParameter In 查询
    SQL 性能优化
    Entity Framework
    【XLL API 函数】 xlfSetName
    【XLL API 函数】xlfUnregister (Form 2)
    【XLL API 函数】xlfUnregister (Form 1)
    【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
    【Excel 4.0 函数】REGISTER
    【Bochs 官方手册翻译】 第一章 Bochs介绍
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5065417.html
Copyright © 2011-2022 走看看