二分法(四):采用二分法解决“最大化平均值”…

2019-08-16 07:50:58来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

二分法(四):采用二分法解决“最大化平均值”问题

【例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\n",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\n",left);

         }

       return 0;

}

 


原文链接:https://www.cnblogs.com/cs-whut/p/11217130.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:带毒的水

下一篇:CF1195E OpenStreetMap