题目链接:https://cn.vjudge.net/problem/UVALive-3635
题意:(蓝书)
思路:
因为答案有单调性,可以用二分答案的方法找出最大的答案;这里要注意的就是,题目的意思是每个人只能分到一种pie,一开始我以为可以把两个完整的pie分给一个人,所以卡了很久;(这里的二分答案挺有用的,建议找找资料学习学习)
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)
int read(){
int r=0,f=1;char p=getchar();
while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}
typedef long long ll;
typedef unsigned long long ull;
const int Maxn = 1e4+10;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 10001;
const double PI = acos(-1.0);
double p[Maxn];
int n,f;
bool ok (double x) {
int cnt = 0; // 单个pi能切出几个x
for (int i = 0; i < n; ++i) cnt+=floor(p[i]/x); // floor 小于参数的最大整数
if(cnt >= f+1) return true;
else return false;
}
int main (void)
{
int t,r;
scanf("%d",&t);
while (t--) {
scanf("%d%d",&n,&f);
double maxn = 0;
for (int i = 0; i < n; ++i) {
scanf("%d",&r);
p[i] = r*r*PI; maxn = max(maxn, p[i]);
}
double le = 0, ri = maxn, mid;
while (ri-le > 1e-5) { // 二分答案的算法,答案单调性
mid = (le+ri)/2;
if(ok(mid)) le = mid;
else ri = mid;
}
printf("%.4f\n",ri);
}
return 0;
}