soj2574: pie
http://acm.scu.edu.cn/soj/problem.action?id=2574
简述题意,有F个客人,N个派,分给F个客人和自己,每个人就一块(不想要碎的两块三块啥的),问最大每个人分多大,输出面积。
因为一个派可以分好几次呀,我太蠢了,直接就分两半了,后来想,哇塞不对啊。先把错的贴出来,反正也没啥人看,自娱自乐。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 10000+10
const double PI = 3.1415926;
int R[MAX];
bool cmp(int a,int b)
{
if(a>=b) return 1;
else return 0;
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int N,F;
scanf("%d%d",&N,&F);
F = F+1;
for(int i = 1; i <= N; i++)
scanf("%d",&R[i]);
double res = 0;
sort(R+1,R+N+1,cmp);
if(N>=F)
{
double tmp = (PI*R[F]*R[F]);
for(int i = 1; i < F; i++)
{
res = (PI*R[i]*R[i])/(F-(i-1));
if(res > tmp)
tmp = res;
}
printf("%.6f\n",tmp);
}
else{
double tmp = (PI*R[1]*R[1])/(F-(N+1));
for(int i = 2; i <= N; i++)
{
res = (PI*R[i]*R[i])/(F-(i-1));
if(res > tmp)
tmp = res;
}
printf("%.6f\n",tmp);
}
}
return 0;
}
ac如下
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define MAX 10000+10
const double PI = atan(1)*4;
const double ask = 1e-7;
double R[MAX];
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int N,F;
scanf("%d%d",&N,&F);
F = F+1;
double high = 0.0,low = 0.0,mid = 0.0;
for(int i = 1; i <= N; i++)
{
scanf("%lf",&R[i]);
R[i] = R[i]*R[i]*PI;
high = max(high,R[i]);
// printf("%lf %lf",high,R[i]);
}
while(high-low > ask)
{
int res = 0;
mid = (high+low)/2;
for(int i = 1;i <= N;i++)
res += (int)(R[i]/mid);
if(res >= F)
low = mid;
else high = mid;
}
printf("%.6lf\n",mid);
}
return 0;
}
就是每个派都可以切好几份,那就是取中等大小,就二分取中间大小的,然后每个派 / 中间大小,取整数之和与人数比,不然万一1.5块+0.5块=3块?其实只能1块啦,客人才不想吃碎碎渣子啦。
然后注意的点是,题目要求
The answer should be given as a floating point number with an absolute error of at most 10^-6.
所以我刚开始ask = 1e^-6,结果却和示例不同,10^-8又会超时,所以10^-7好了,真是艰辛,挨个试。。。哈哈
再有一点,double是双精度,要用%lf,float才用%f希望大家别用错了。
哎呀我的妈,虽然是一道简单题,没想到我排第一了!!!天哪!!!我好久以前其他号做也没拿到第一~~
留图,以后看看还可以开心一下