UVA 1476 - Error Curves(三分法)
2014-07-23 17:15
344 查看
UVA 1476 1476 - Error Curves
题目链接题意:给几条下凹二次函数曲线,然后问[0,1000]所有位置中,每个位置的值为曲线中最大值的值,问所有位置的最小值是多少
思路:三分法,由于都是下凹函数,所以所有曲线合并起来,仍然是一个下凹函数,满足单峰,用三分求极值
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 10005;
int t, n, ans;
struct Line {
double a, b, c;
} l
;
double cal(double x) {
double ans = l[0].a * x * x + l[0].b * x + l[0].c;
for (int i = 1; i < n; i++)
ans = max(ans, l[i].a * x * x + l[i].b * x + l[i].c);
return ans;
}
double solve() {
double l = 0, r = 1000;
while (fabs(l - r) > 1e-9) {
double ml = (2 * l + r) / 3;
double mr = (l + 2 * r) / 3;
if (cal(ml) < cal(mr)) r = mr;
else l = ml;
}
return cal(l);
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf%lf", &l[i].a, &l[i].b, &l[i].c);
printf("%.4lf\n", solve());
}
return 0;
}
相关文章推荐
- UVA 1476/HDU 3714/ZOJ 3421 Error Curves 三分法
- UVA 1476 - Error Curves(三分法)
- UVa1476 Error Curves
- uva 1476 - Error Curves
- uva 1476 - Error Curves(三分)
- 【单峰函数,三分搜索算法(Ternary_Search)】UVa 1476 - Error Curves
- uva1476 Error Curves
- UVa 1476 - Error Curves
- UVA1476——Error Curves (三分求极值)
- UVA 1476 Error Curves
- UVALive - 5009 Error Curves (三分法)
- HDU 3714/UVA1476 Error Curves
- uva 1476 Error Curves(三分)
- UVA - 1476 Error Curves (三分搜索)
- UVA 1476 A - Error Curves——三分
- Uva - 1476 - Error Curves(三分)
- 三分法专练 hdu - Error Curves-3714 & hdu - The Moving Points-4717
- zju 3421 Error Curves(凸函数最值,三分法)
- LA 5009 (三分法求极值) Error Curves
- uvalive 5009 Error Curves