您的位置:首页 > 其它

HDOJ Error Curves 计算几何

2012-03-31 20:28 330 查看
题意:在给定的n个抛物线中,求 min(F(x)), F(x) = max(S(x)).

思路:由于给定的抛物线都是开口向上的(0 ≤ a ≤ 100)F(x)也是开口向上的类抛物线,具有单峰值,利用三分求解 。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

#define eps 1e-10
const int MAX = 10009;

struct para {
int a, b, c;
};

para p[MAX];
int n;

double getmax(double x) {
double ans, tmp;
ans = p[0].a*x*x + p[0].b*x + p[0].c;
int i;
for (i = 1; i < n; ++i) {
tmp = p[i].a*x*x + p[i].b*x + p[i].c;
if (ans < tmp)
ans = tmp;
}
return ans;
}

int main()
{
int t;
double l, r, mid1, mid2, max1, max2;
scanf("%d",&t);
while (t--) {
scanf("%d", &n);
int i;
for (i = 0; i < n; ++i)
scanf("%d %d %d", &p[i].a, &p[i].b, &p[i].c);
l = 0.0;
r = 1000.0;
while (fabs(r-l) > eps) {
mid1 = (l+r)/2;
mid2 = (l+mid1)/2;
max1 = getmax(mid1);
max2 = getmax(mid2);
if (max1 > max2)
r = mid1;
else
l = mid2;
}
printf("%.4lf\n", max1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: