HDU 1969: Pie

本文提供了一种解决HDU 1969问题的有效算法。通过二分查找法来确定能够切分的最大蛋糕体积,确保每个朋友都能获得至少一份。代码中详细展示了输入输出流程及关键步骤。


///@link http://acm.hdu.edu.cn/showproblem.php?pid=1969
///@author Sycamore
///@date Aug 16
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-5,PI=acos(-1);
#define endl '\n'
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,f;
cin>>n>>f;
f++;
double total=0;
vector<double>volume(n);
for(auto &e:volume)
{
cin>>e;
e*=e*PI;
total+=e;
}
double low=0,high=total/f,mid;
while(low+eps<=high)
{
long long cnt=0;
mid=(low+high)/2;
for(auto &e:volume)
cnt+=int(e/mid);//notice here! 9 WAs in 17 min!
//int can be floor
//consider the expression cnt+=e/mid
//several events happen as follows
//1)double tmp==(double)cnt+e/mid;
//2)cnt=(int)tmp;
//the extra errors yield when cnt is cast to double type
//and then added by the value of e/mid
if(cnt>=f)low=mid;
else high=mid;
}
cout<<fixed<<setprecision(4)<<mid<<endl;
}
return 0;

 

转载于:https://www.cnblogs.com/zjnu/p/7376190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值