简单三分
作者:互联网
题目:
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?
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.
题目大意:
Josephina是一个聪明的女孩,最近沉迷于机器学习。她
非常关注一种称为线性判别分析的方法,该方法
具有许多有趣的特性。
为了测试算法的效率,她收集了许多数据集。
而且,每个数据分为两部分:训练数据和测试
数据。她在训练数据上获取模型的参数,并
在测试数据上对模型进行测试。令她惊讶的是,她发现每个数据集的测试误差曲线只是一条抛物线。抛物线对应于二次函数。在数学中,二次函数是形式为f(x)= ax2 + bx + c的多项式函数。如果a = 0,则二次方将降级为线性函数。
如果只有一条测试误差曲线,则计算最小误差非常容易。但是,有多个数据集,这意味着Josephina将获得许多抛物线。约瑟芬娜(Josephina)希望获得在所有数据集上都能获得最佳性能的调整参数。因此,她应该考虑所有误差曲线,即,她必须处理许多二次函数并制定新的误差定义以表示总误差。现在,她将重点放在与多个二次函数相关的以下新函数的最小值上。新函数F(x)定义如下:F(x)= max(Si(x)),i = 1 … n。x的域是[0,1000]。Si(x)是二次函数。约瑟夫娜(Josephina)想知道F(x)的最小值。不幸的是,她很难解决这个问题。作为一名超级程序员,您能帮她吗?
题解:
一道三分题,我们可以直接套三分的公式,做出来很容易,但要注意一些小问题,判断时r-l时要取小但不能取0,因为会有精度问题。我们令mid=(r+l)/2,www=(mid+r)/2,如果mid<www,很容易判断最小值位于www和l之间,因而r=www,另一种情况也易判断,最小值位于mid和www之间,因而l=mid,循环下去即可解题。
代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
double a[10000],b[10000],c[10000];
int n;
double f(double x)
{
double ans=a[0]*x*x+b[0]*x+c[0];
for(int i=1;i<n;i++)
{
if(ans<a[i]*x*x+b[i]*x+c[i]) ans=a[i]*x*x+b[i]*x+c[i];
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i];
double l=0.0,r=1000.0;
while(r-l>1e-10)
{
double mid=(l+r)/2;
double www=(mid+r)/2;
if(f(www)>f(mid)) r=www;
else l=mid;
}
printf("%.4lf\n",f(l));
}
return 0;
}
hjhj666666
发布了8 篇原创文章 · 获赞 0 · 访问量 45
私信
关注
标签:function,double,error,Josephina,mid,简单,test,三分 来源: https://blog.csdn.net/hjhj666666/article/details/103983439