POJ 3122 - Pie (最大化最小值)
Time Limit Exceeded,而不是因为数据要求高而超时。
2.由于精度要求特别特别高,所以PI = 3.1415926535897932 和 high - low > 0.000001 是必
须的,也是这道题总是WA的地方。
double mid, low = 0, high = sum / f;
while(high - low > 0.000001){
mid = (low + high) / 2;
int count = 0; // count为当前mid值对应的能分给的人数。
for(i = 1; i <= n; i ++)
count += (int)(pie[i] / mid);
if(count >= f) low = mid;
else high = mid;
}
printf("%.4lf\n", mid * PI);
}
return 0;
}
http://blog.csdn.net/HJ_Air/article/details/7360463
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.
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.2655http://blog.sina.com.cn/s/blog_6635898a0100ko9l.html
题意:作者要开一个生日party,他现在拥有n块高度都为1的圆柱形奶酪,已知每块奶酪的底面半径为r不等,作者邀请了f个朋友参加了他的party,他要把这些奶酪平均分给所有的朋友和他自己(f+1人),每个人分得奶酪的体积必须相等(这个值是确定的),形状就没有要求。现在要你求出所有人都能够得到的最大块奶酪的体积是多少?
思路:贪心的思想+二分。复杂度为O(nlogM),M为初始时的high-low。初始状态,上界high为每个人分得奶酪的体积sum,下界low = 0(或者是最小块的奶酪)。然后二分,每次的mid值为(low+high)/ 2,然后根据mid值(估计值)遍历n块奶酪,看看这个mid值能分给多少个人,如果份数大于等于f,表示mid偏小,low = mid,反之high = mid。
注意的问题: 1.数据得开double,开float的话精度不够,二分时 high - low > 0.000001 会出不来,导致
int main(){
int t, n, f, i;
double pie[Max];
scanf("%d", &t);
while(t --){
scanf("%d%d", &n, &f);
f ++; // 加上作者一人。
double sum = 0;
for(i = 1; i <= n; i ++){
int r;
scanf("%d", &r);
pie[i] = r * r;
sum += pie[i];
}
}