hdu Pie

这道题是一道二分搜索的题,首先计算出最大的平均体积:mx=V总/f;然后去left=0,right=mx,mid=(left+right)/2进行二分搜索,当所有pi分割出的mid的个数是大于等于f时,说明这个mid是满足条件的,一直搜索知道找到最大的mid;这题的核心还是精度的问题。不得不说这题对精度的要求实在是太高了。首先是mi的精度,当我用的是mi=1e-8时,会超时,慢慢将mi降到1e-6。后来又发现当将pi定义为3.1415926535是个会wa掉,后来参考了一下别人的代码才知道原来double pi = acos(-1.0) 这样定义pi精度会更高。。。也是醉了。

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"cmath"
#define mi 1e-6
//#define pi 3.1415926535
using namespace std;
double pi = acos(-1.0);
double v[10005];
int f,n;
bool test(double x)
{
    int i,num=0;
    for(i=0;i<n;i++)
    {
        num+=(int) (v[i]/x);
    }
    if(num>=f) return true;
    else return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j;
        cin>>n>>f;
        f++;
        double r,left,right,mid,mx,V=0;
        for(i=0;i<n;i++) {cin>>r;v[i]=pi*r*r;V+=v[i];}
        mx=V/f;
        left=0;right=mx;
        while(right-left>mi)
        {
            mid=(right+left)/2.0;
            if(test(mid)) left=mid;
            else right=mid;
        }
        printf("%.4lf\n",mid);
    }
    return 0;
}

时间: 2024-08-07 23:25:13

hdu Pie的相关文章

hdu 1969 Pie(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1969 Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4513    Accepted Submission(s): 1819 Problem Description My birthday is coming up and trad

HDU 3392 Pie(DP+滚动数组)

题意:有一些男生女生,男生女生数量差不超过100 ,男生女生两两配对.要求求出一种配对方法,使每一对的高度差的和最小. 思路:(我是真的笨笨笨!!磨磨唧唧写一堆是因为我笨!我看了别人的博客,思路全是学别人的,轻喷!)设人少的一组人数为n,b[],人多的一组人数为m,g[](b[],g[]先排好序),用dp[i][j]表示n中的前i个人与m中的前j个人配对所得到的最小值. 那么dp[i][j]就是min(dp[i-1][k]+|b[i]-g[k]|),就是n中前i-1个人和m中前1~k个人配对的最

HDU 1969 Pie(二分搜索)

题目链接 Problem 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.

hdu 1969 Pie

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6752    Accepted Submission(s): 2564 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I h

HDU 3392 Pie(滚动数组优化)

Problem Description A lot of boys and girls come to our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone's height, and we believe that the less difference of a girl and a boy has,

Pie HDU杭电1969 【二分】

Problem 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

HDU 1969 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

hdu 1969 Pie(贪心+二分查找)(简单)

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5628    Accepted Submission(s): 2184 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I

HDU 1969 Pie 二分

1.题意:一项分圆饼的任务,一堆圆饼共有N个,半径不同,厚度一样,要分给F+1个人.要求每个人分的一样多,圆饼允许切但是不允许拼接,也就是每个人拿到的最多是一个完整饼,或者一个被切掉一部分的饼,要求你算出每人能分到的饼的体积最大值.输入数据依次给出,测试数据组数T,每组数据中,给出N,F,以及N个圆饼的半径.输出最大体积的数值,精确到小数点后四位. 2.分析:一看是这种输出就知道用二分写会很高效,这里对"能分出的最大体积值"进行二分.首先,这个值有界,最大值为总体积除以总人数的值,即Σ