我是靠谱客的博主 羞涩樱桃,最近开发中收集的这篇文章主要介绍POJ 3122 Pie(二分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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



题目大意:

        我有f个朋友,n个派,每个派有不同的半径,开趴的时候,给每个朋友包括我自己都分一块派,这块派必须来自一个派的一部分(也就是说不接受两块或两块以上来自不同派的块),并且所有人分得的派的面积要相等,形状不限,问最多每人能分得多大面积的派

解题思路:

二分枚举答案,注意一下精度就好


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>

#define STD_REOPEN() freopen("../in.in","r",stdin)
#define STREAM_REOPEN fstream cin("../in.in")
#define INF 0x3f3f3f3f
#define _INF 63
#define eps 1e-5
#define MAX_V 100010
#define MAX_P 110
#define MAX_E 10010
#define MAX 32000
#define MOD_P 3221225473
#define MOD 9901

using namespace std;

int t,n,f;
double s[10010];
const double PI=acos(-1.0);

int dcmp(double x)
{
	return fabs(x)<eps?0:(x<0?-1:1);
}

int main()
{
	//STD_REOPEN();
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&f);
		for(int i=0;i<n;i++)
		{
			scanf("%lf",&s[i]);
			s[i]=s[i]*s[i];	//为了避免精度问题的出现,这里暂时不乘PI
		}
		f++;
		double l=0,r=200000010.0;
		double ans=-1.0;
		while(dcmp(r-l)>0)
		{
			double mid=(l+r)/2;
			int cnt=0;
			for(int i=0;i<n;i++)
				cnt+=s[i]/mid;	//记录当前面积可以分给多少人
			if(cnt<f)	//小于总人数,面积过大,向左枚举
				r=mid;
			else	//否则向右枚举
			{
				ans=max(ans,mid);
				l=mid;
			}
		}
		printf("%.4fn",ans*PI);
	}

    return 0;
}


最后

以上就是羞涩樱桃为你收集整理的POJ 3122 Pie(二分)的全部内容,希望文章能够帮你解决POJ 3122 Pie(二分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部