HDU-1969 Pie

来源:互联网 发布:ubuntu 14.04与16.04 编辑:程序博客网 时间:2024/06/05 18:38

Pie

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

大神的解释:

题目大意是要办生日Party,有n个馅饼,有f个朋友,接下来是n个馅饼的半径。然后是分馅饼了,

注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中

各割一小块来凑一块,像面积为10的和6的两块饼(饼的厚度是1,所以面积和体积相等),

如果每人分到面积为5,则10分两块,6切成5,够分3个人,如果每人6,则只能分两个了!

题目要求我们分到的饼尽可能的大!

 

只要注意精度问题就可以了,一般WA都是精度问题

运用2分搜索:

首先用总饼的体积除以总人数,得到每个人最大可以得到的V,但是每个人手中不能有两片或多片拼成的一块饼,

最多只能有一片分割过得饼。用2分搜索时,把0设为left,把V设为rightmid=(left+right)/2;

搜索条件是:以mid为标志,如果每块饼都可以分割出一个mid,那么返回true,说明每个人可以得到的饼的体积可以

大于等于mid;如果不能分出这么多的mid,那么返回false,说明每个人可以得到饼的体积小于等于mid

1)精度为:0.000001

(2)   pi 用反余弦求出,精度更高。

 

 我的代码:

#include<stdio.h>

#include<math.h>

#include<string.h>

#include<algorithm>

double pi=acos(-1.0);//话说这个地方不用会WA的。。。

using namespace std;

double a[10086];

double m,n;

bool hanshu(double x)

{

    int s=0,i;

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

    {

        s=s+int(a[i]/x);//判断每个饼能否被整分强制类型转化,必须是整数

    }

    if(s>=n)//大于人数,饼够

        return 1;

    else return 0;

}

int main()

{

int T;

scanf("%d",&T);

while(T--)

{

double sum=0,v1;

int i,r;

scanf("%lf%lf",&m,&n);

n=n+1;

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

        {

            scanf("%d",&r);

            a[i]=r*r*pi;//每个饼的面积

            sum=sum+a[i];//饼的总面积

        }

        v1=sum/n;//最大可能分得的饼的面积

        double low,high,mid;

        low=0.0;

        high=v1;

        while((high-low)>=1e-6)

        {

            mid=(low+high)/2;

            if(hanshu(mid))

                low=mid;

            else

                high=mid;

        }

        printf("%.4lf\n",mid);

}

return 0;

}

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 评书杨家将刘兰芳播讲全集 刘兰芳评书大全在线听 刘兰芳评书呼家将93回 呼家将评书刘兰芳全集 刘兰芳评书网赵匡胤 呼家将刘兰芳评书全集 刘兰芳评书赵匡胤演义 刘兰芳评书网在线收听 刘兰芳赵匡胤演义100回 刘兰芳评书在线收听 刘兰芳评书赵匡胤演义100 刘兰芳岳飞传全集mp3在线听 刘兰芳评书大全岳飞传 评书赵匡胤刘兰芳播讲 扬家将评书刘兰芳播讲 岳飞传刘兰芳全集117 刘兰芳评书杨家将93回在线听 评书岳飞传刘兰芳播讲117回 刘兰芳评书赵匡胤演义100回 刘兰芳评书杨家将全集 刘兰芳评书杨家将全传 刘兰芳评书岳飞全传 刘兰芳评书岳飞传大全117集 刘兰芳评书网赵匡胤演义 刘兰芳平书杨家将全集 刘兰芳评书呼家将全集 评书杨家将刘兰芳广播全集 评书网刘兰芳岳飞传 评书杨家将刘兰芳mp3下载 评书岳飞传刘兰芳全集 刘兰芳岳飞传全集mp3下载 评书网刘兰芳杨家将 刘兰芳的评书岳飞传 刘兰芳评书大全呼家将 刘兰芳评书打包下载 杨家将评书刘兰芳全集下载 评书呼家将刘兰芳 全集 刘兰芳评书下载mp3打包下载 刘兰芳评书呼家将征西 刘兰芳评书大全杨家将 刘兰芳评书岳飞传全集mp3