题目:http://poj.org/problem?id=3122
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:
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
分析:dang'a有n块蛋糕,每块蛋糕的大小和口味不同,要把n块蛋糕分给(f+1)个人,每个人分的蛋糕大小相同,并且不同口味蛋糕的不能拼凑,可以浪费,问能把蛋糕分给(f+1)个人的最大体积是多少。利用二分答案,下界取0.0,上界取最大的蛋糕体积,然后枚举体积X判断,丢弃比x小的,利用lower_bound()函数,找到第一个大于等于x的第一个体积,之后开始检验每一个,将他们能分成的体积个数加起来,如果大于(f+1)返回true,否则返回false。不过pi 要尽可能取准确,利用math库的acos(-1.0)可以取准确。
16ms代码:
#include
#include
#include
#include
#include
#define pi acos(-1.0)
using namespace std;
const int maxn = 10005;
int r[maxn];
double v[maxn];
int n,f;
bool check(double x)
{
int cnt = 0;
int t = lower_bound(v,v + n,x) - v;
for(int i = t;i < n;i ++)
{
cnt += (int)(v[i] / x);
}
if(cnt >= f+1)return true;
else return false;
}
double binarySearch(double left,double right)
{
double ans;
double l = left,r = right;
while(l + 1e-4 < r)
{
double mid = (l + r) / 2;
if(check(mid))
{
ans = mid;
l = mid;
}
else r = mid;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&f);
for(int i = 0;i < n;i ++)
{
scanf("%d",&r[i]);
v[i]= pi * r[i] * r[i] * 1;
}
if(n == 1)printf("%.4lf\n",v[0] / (f + 1));
else {
sort(v,v + n);
double left = 0.0,right = v[n-1];
printf("%.4lf\n",binarySearch(left,right));
}
}
return 0;
}
32ms代码:
#include
#include
#include
#include
#include
#define pi acos(-1.0)//pi 尽可能准确。
using namespace std;
const int maxn = 10005;
int r;
double v[maxn];
int n,f;
bool check(double x)
{
int cnt = 0;
int t = lower_bound(v,v + n,x) - v;
for(int i = t;i < n;i ++)
{
cnt += (int)(v[i] / x);
}
if(cnt >= f+1)return true;
else return false;
}
double binarySearch(double left,double right)
{
double ans;
double l = left,r = right;
while(l + 1e-7 < r)
{
double mid = (l + r) / 2;
if(check(mid))
{
ans = mid;
l = mid;
}
else r = mid;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double left = 0.0,right = 0.0;
scanf("%d%d",&n,&f);
for(int i = 0;i < n;i ++)
{
scanf("%d",&r);
v[i] = pi * r * r * 1;
right += v[i];
}
right /= (f+1);
if(n == 1)printf("%.4lf\n",v[0] / (f + 1));
else {
sort(v,v + n);
printf("%.4lf\n",binarySearch(left,right));
}
}
return 0;
}