http://acm.hdu.edu.cn/showproblem.php?pid=1969
提议为n个人分f块蛋糕,而且每个人只能从一个蛋糕中一块,问每个人分得的最大蛋糕的面积
二分 从中间向左右查找最大值
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
double pie[11000];
const double pi = acos(-1.0);
int main()
{
int n,f,t,r;
double s = 0,sum,mmax;
scanf("%d",&t);
while(t--)
{
mmax = 0;
sum = 0;
scanf("%d %d",&n,&f);
f++;
for(int i = 0; i < n; ++i)
{
scanf("%d",&r);
pie[i] = pi*r*r;
}
sort(pie,pie+n);
int cnt;
double mid,l = 0,r = pie[n-1];//左边界和又边界分别为一个人最大和最小分得的
while((r-l)> 1e-6)
{
cnt = 0;
mid = ((l+r)/2);
for(int i = 0; i < n; ++i)
{
cnt = cnt + pie[i]/mid;
}
if(cnt >= f)
{
mmax = max(mmax,mid);
l = mid;
}
else
r = mid;
}
printf("%.4lf\n",mmax);
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2199
题意:给出y的值求x的值
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
double fun(double x)
{
return 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;
}
int main()
{
int t;
double mid,a;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&a);
if(fun(100) < a || a < 6)
{
printf("No solution!\n");
}
else
{
double l = 0,r = 100;
while(r - l > 1e-6)
{
mid = (l + r)/2;
if(fun(mid) > a)
r = mid;
else if(fun(mid) < a)
l = mid;
}
printf("%.4lf\n",mid);
}
}
return 0;
}