zoukankan      html  css  js  c++  java
  • 二分法(四):采用二分法解决“最大化平均值”问题

    【例1】切绳子。

    题目描述

    有N条绳子,它们的长度分别为Li。如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位(直接舍掉2位后的小数)。

    输入输出格式

    输入格式:

    第一行两个整数N和K(0<N<=10000, 0<K<=10000),接下来N行,描述了每条绳子的长度Li(0<Li<=100000.00)。

    输出格式:

    切割后每条绳子的最大长度。

    输入输出样例

    输入样例#1: 

    4 11

    8.02

    7.43

    4.57

    5.39

    输出样例#1:

    2.00

          (1)编程思路。

          先确定初始区间[left,right],显然left=0,right可取单段绳子的最大值,然后使用二分法得到mid,测试mid是大于要求的值还是小于要求的值。如果mid值可以切出k段绳子,则增大下界,使left=mid;如果mid值切不出k段绳子,则mid取大了,减小上界,使right=mid;.然后最终使left 与right无限逼近答案。注意:程序中可以使用 while ((right-left)>1e-4)作为循环控制条件。

          (2)源程序。

    #include <stdio.h>

    #include <math.h>

    #define MAX_N 10000

    double a[MAX_N+1];

    int n,k;

    int judge(double x)

    {

             int num = 0,i;

             for (i = 0; i < n; i++)

                       num += (int)(a[i] / x);

             return num;

    }

    int main()

    {

        int i;

        double left,right,mid,max=0;

             scanf("%d%d",&n,&k);

        for(i=0;i<n;i++)

        {

                scanf("%lf",&a[i]);

                if (max<a[i]) max=a[i];

        }

        left=0, right=max;

        while ((right-left)>1e-4)

        {

            mid=(left+right)/2;

            if(judge(mid)>=k) left=mid;

            else right=mid;

        }

        printf("%.2f ",floor(right*100)/100);

        return 0;

    }

    将此源程序提交给POJ 1064 “Cable master”,可以Accepted。

    【例2】Pie (POJ 3122)。

    Description

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

    My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

    What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

    Input

    One line with a positive integer: the number of test cases. Then for each test case:

    One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.

    One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.

    Output

    For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10−3.

    Sample Input

    3

    3 3

    4 3 3

    1 24

    5

    10 5

    1 4 2 3 4 5 6 5 4 2

    Sample Output

    25.1327

    3.1416

    50.2655

          (1)编程思路。

          题目要求是公平地分披萨Pie。

          每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就是所有人得到的pie尺寸一致,但是形状可以不同),而且每个人得到的那份pie必须是从同一个pie上得到的。注意,由于每个人只能从一个pie上分,因此公平分Pie,不是简单求个平均值,而是要寻找到平均值的最大值。

          例如,有3个pie, 尺寸分别为1,2,3。

          如果要给每人尺寸为2的pie,那么最多分给2个人,而不是3个人。因为第一个pie尺寸为1,小于2,扔掉;第二个pie尺寸为2,等于2,刚好分给一个人;第三个pie尺寸为3,切出尺寸为2的一份,分给一个人,剩下的尺寸为1的就扔掉。注意:第1个pie和第3个pie剩余的合起来好像正好可以分给1人,但不能这样做。如果这样做,就变成单纯地求平均值了。

          同样采用二分法解决这个最大化平均值问题。

          令下界left=0,即每人都分不到pie,上界right=Sum/(f+1),即每人都得到了算术平均值,实际上难做到,只可能在n个人分n个pie,每个pie还等大时出现。

           使用二分法得到mid,计算如果按照mid的尺寸分pie,能分给多少人。如果当前mid值可以将pie分给f+1个人,则增大下界,使left=mid;如果按mid值分不了f+1人,则mid取大了,减小上界,使right=mid;.然后最终使left 与right无限逼近答案。注意:程序中可以使用 while ((right-left)>1e-6)作为循环控制条件。

          (2)源程序。

    #include <stdio.h>

    #include <math.h>

    const double PI = 3.1415926535898;

    double pie[100010];

    int n,f;

    bool judge(double x)

    {

             int num = 0,i;

             for (i = 0; i < n; i++)

                       num += (int)(pie[i] / x);

             return num >= f;

    }

    int main()

    {

        int t,r;

        double sum,left,right,mid;

        scanf("%d",&t);

        while(t--)

        {

            sum=0;

            scanf("%d %d",&n,&f);

            f++;

            for(int i = 0 ; i < n ; i++)

            {

                scanf("%d",&r);

                pie[i]=r*r*PI;

                sum+=pie[i];

            }

            left=0.0;

            right=sum/f;

            while (fabs(right-left)>1e-6)

            {

                mid=(left+right)/2;

                if (judge(mid))

                {

                    left=mid;

                }

                else

                {

                    right=mid;

                 }

             }

             printf("%.4f ",left);

             }

           return 0;

    }

  • 相关阅读:
    Usb key插入检测,并动态获取CSP
    PsGetVersion
    BCDEDIT 启动配置数据存储编辑器
    fatal error LNK1103: debugging information corrupt; recompile module 解决
    Smart Card(windows)
    findwindow 后postmessage点解按钮
    核心层API分类
    64位下SetWindowLong时的参数GWL_WNDPROC undeclared identifier未定义的错误
    安装SQL SERVER 2008时出现了SQL SERVER 2005 Express Tool Installed 的错误
    win7重置密码的方法
  • 原文地址:https://www.cnblogs.com/cs-whut/p/11217130.html
Copyright © 2011-2022 走看看