Error Curves HDU

来源:互联网 发布:2013excel不能粘贴数据 编辑:程序博客网 时间:2024/05/21 14:57

Josephina is a clever girl and addicted to Machine Learning recently. She
pays much attention to a method called Linear Discriminant Analysis, which
has many interesting properties.
In order to test the algorithm’s efficiency, she collects many datasets.
What’s more, each data is divided into two parts: training data and test
data. She gets the parameters of the model on training data and test the
model on test data. To her surprise, she finds each dataset’s test error curve is just a parabolic curve. A parabolic curve corresponds to a quadratic function. In mathematics, a quadratic function is a polynomial function of the form f(x) = ax2 + bx + c. The quadratic will degrade to linear function if a = 0.
这里写图片描述

It’s very easy to calculate the minimal error if there is only one test error curve. However, there are several datasets, which means Josephina will obtain many parabolic curves. Josephina wants to get the tuned parameters that make the best performance on all datasets. So she should take all error curves into account, i.e., she has to deal with many quadric functions and make a new error definition to represent the total error. Now, she focuses on the following new function’s minimum which related to multiple quadric functions. The new function F(x) is defined as follows: F(x) = max(Si(x)), i = 1…n. The domain of x is 0,10000,1000. Si(x) is a quadric function. Josephina wonders the minimum of F(x). Unfortunately, it’s too hard for her to solve this problem. As a super programmer, can you help her?
Input
The input contains multiple test cases. The first line is the number of cases T (T < 100). Each case begins with a number n (n ≤ 10000). Following n lines, each line contains three integers a (0 ≤ a ≤ 100), b (|b| ≤ 5000), c (|c| ≤ 5000), which mean the corresponding coefficients of a quadratic function.
Output
For each test case, output the answer in a line. Round to 4 digits after the decimal point.
Sample Input
2
1
2 0 0
2
2 0 0
2 -4 2
Sample Output
0.0000
0.5000

第一次写三分的题。
这道题的意思是给出一元二次函数,求在0 到 1000 的范围内,每个 x 对应的最大值 的最小,题目有点绕口,仔细理解下就知道了,对于每个 x 都可以找到一个最大值,求在这些最大值中最小的是哪个。

三分法 :用来求凹型或者凸型的最值,对于左右端点可以得到 m = (l + r) / 2
mm = ( m + r) / 2; 如果 f(m) < f(mm) ,则说明最小值在m 的左端,如果f(m) > f(mm) ,则说明最小值在mm 的右端。

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<queue>#include<algorithm>#define ll long long#define inf 0x3f3f3f3f#define exp 0.000000001const int m = 100007;using namespace std;int n;int a[m],b[m],c[m];double dis(double x )// 对于所有的a,b,c,求出最大值{    double maxn = -inf;      double tmp;    for(int i=0; i<n; i++)    {        tmp = a[i] * x * x + b[i] * x + c[i];        if( tmp > maxn )            maxn = tmp;    }    return maxn;}double  solve(){    double l = 0.0,r = 1000.0;    double m,mm,t1,t2;    while( r - l > exp )    {        m = (l + r) / 2.0;        mm = (m + r) / 2.0;        t1 = dis(m);        t2 = dis(mm);//      printf("t1 = %.4lf  t2 =  %.4lf m = %.4lf  mm = %.4lf\n",t1,t2,m,mm);        if( t1 > t2)    l = m;        else    r = mm;    }    return t1;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%d%d%d",&a[i],&b[i],&c[i]);        double ans = solve();           printf("%.4lf\n",ans);    }    return 0;}
0 0