HDU-3714 Error Curves 题解*

题面:

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, 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?

输入:

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.

输出:

For each test case, output the answer in a line. Round to 4 digits after the decimal point.

题目翻译:

设函数F(x) = max{F1(x) + F2(x) + … + Fn(x)},找出F(x)的最小值.
Fi(x)为二次函数

输入:

第一行为一个数字t,之后输入t组数据.
每组数据由n+1行组成
第一行为n,代表F(x)由多少个二次函数组成.
之后n行包含三个数字,分别为二次函数二次项,一次项,常数项的系数.

输出:

每组数据输出一个答案,F(x)的最小值.结果保留4位小数.

题目分析:

先定义一个函数fmax,用来求出F(x).
然后使用三分法
设right = 1000, left = 0.
当left + (right-left)/3的函数值大于right - (right - left)/3的函数值时,说明答案在left+(right-left)/3和right之间.这时left更新为left+(right-left0)/3.
否则right更新为right-(right-left)/3.

代码:

#include<stdio.h>
#include<math.h>
int t, n;
int c[10000][3];
double right, left;
double fmax(double x){
        double max = c[0][0]*x*x + c[0][1]*x + c[0][2];
        for(int i = 1; i < n; i++){
                if(max < c[i][0]*x*x + c[i][1]*x + c[i][2]){
                        max = c[i][0]*x*x + c[i][1]*x + c[i][2];
                }
        }
        return max;
}
int main(){
        scanf("%d", &t);
        while(t>0){
                scanf("%d", &n);
                for(int i = 0; i < n; i++)
                        scanf("%d%d%d", &c[i][0], &c[i][1], &c[i][2]);
                left = 0;
                right = 1000;
                for(int i = 0; i < 100; i++){
                        if(fmax(left + (right - left)/3) > fmax(right - (right - left)/3)){
                                left = left + (right - left)/3;
                        }else{
                                right = right - (right - left)/3;
                        }
                }
                printf("%.4f\n", fmax(left));
                t--;
        }
}

猜你喜欢

转载自blog.csdn.net/qq_25807093/article/details/86703036