来源:http://poj.org/problem?id=3122
POJ3122
就是说现在有N个饼,F个朋友来分,要求(F+1)个能分的一块饼上的一块,不能来自不同的饼,这个题目就明显多了,就是估计大概的饼的面积,然后分了试试看,模拟进行和人数进行比较。
技巧:
1.这里POJ的DISCUSS中提到,这里pi取3.1415926不够,所以这里可以用pi=acos(-1.0)这样,可以规避这个错误。
2.double类型不要用l<=r,而是l-r>1e-6来判断是否应该结束。当然,这里的精度要求没有这么高。
3.最后要用.f提交,这是题目要求。。。可怕的要求,不过好多人WA在这了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int Maxn=10010;
const double pi=acos(-1.0);
double a[Maxn];
int judge(int n,int f,double s){
int cnt=0,i;
for(i=0;i<n;i++){
cnt+=(int)floor(a[i]/s);
if(cnt>=f) return 1;
}
return 0;
}
bool cmp(double a,double b){
return a>b;
}
int main(){
int c;
int n,f;
double s,mp;
double ans;
scanf("%d",&c);
while(c--){
ans=s=0;
memset(a,0,sizeof(a));
scanf("%d%d",&n,&f);
f++;
for(int i=0;i<n;i++){
scanf("%lf",&a[i]);
a[i]=a[i]*a[i]*pi;
s+=a[i];
}
double r,l;
sort(a,a+n,cmp);
//cout<<a[0]<<" "<<a[n-1]<<endl;
r=s/(double)f;
l=a[0]/(double)f;
while(r>1e-5+l){
double mid=(r-l)/2+l;
if(judge(n,f,mid)){
l=mid;
}
else{
r=mid;
}
}
printf("%.4f\n",l);
}
return 0;
}
AC代码(VJ提交,非POJ):