poj3122
题意:给出若干个pie的半径,从这些pie中切出m个大小相等形状任意的块。可以剩余边角余料并扔掉。问每块最大多大。
分析:这题是浮点数的二分题,二分每块的大小。由于pi要乘以一个较大数,所以精度要求较高,要用acos(-1.0)。这里eps只是在二分比较的时候用了,所以不用设置得太高。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
usingnamespace std;
#define maxn 10005
#define eps 1.0e-4
#define PI acos(-1.0)
int n, m;
int f[maxn];
double s[maxn], ms;
void input()
{
scanf("%d%d", &n, &m);
m++;
for (int i =0; i < n; i++)
scanf("%d", &f[i]);
ms =-1;
for (int i =0; i < n; i++)
{
s[i] = f[i] * f[i] * PI;
ms = max(s[i], ms);
}
}
bool ok(double a)
{
int d =0;
for (int i =0; i < n; i++)
d += (int) (s[i] / a);
return d >= m;
}
double binarysearch()
{
double l = eps;
double r = ms;
double mid;
while (l + eps < r)
{
mid = (l + r) /2;
if (ok(mid))
l = mid;
else
r = mid;
}
return l;
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
double ans = binarysearch();
printf("%.4f\n", ans);
}
return0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
usingnamespace std;
#define maxn 10005
#define eps 1.0e-4
#define PI acos(-1.0)
int n, m;
int f[maxn];
double s[maxn], ms;
void input()
{
scanf("%d%d", &n, &m);
m++;
for (int i =0; i < n; i++)
scanf("%d", &f[i]);
ms =-1;
for (int i =0; i < n; i++)
{
s[i] = f[i] * f[i] * PI;
ms = max(s[i], ms);
}
}
bool ok(double a)
{
int d =0;
for (int i =0; i < n; i++)
d += (int) (s[i] / a);
return d >= m;
}
double binarysearch()
{
double l = eps;
double r = ms;
double mid;
while (l + eps < r)
{
mid = (l + r) /2;
if (ok(mid))
l = mid;
else
r = mid;
}
return l;
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
double ans = binarysearch();
printf("%.4f\n", ans);
}
return0;
}