Error Curves(2010成都现场赛题)

F - Error Curves

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

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 ifa = 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 minimal which related to multiple quadric functions.

The new function F(x) is defined as follow:

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.

Sample Input

2
1
2 0 0
2
2 0 0
2 -4 2

Sample Output

0.0000
0.5000

简单三分。

可以证明,许多二次函数f取其最大即F(x)=max(f(x))依然为下凸函数,类似二次函数。

便可进行三分处理。

#include<cmath>
#include<iostream>
#include<cstdio>
#define max(x,y) ((x)<(y)?(y):(x))
double l,r,a[10005],b[10005],c[10005],mid,mmid;
int n;
double f(double x){
    double ans=-1000000000;
    for(int i=1;i<=n;i++)
        ans=max(ans,a[i]*x*x+b[i]*x+c[i]);
    return ans;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf",&a[i],&b[i],&c[i]);
        l=0;r=1000;
        while(r-l>1e-10){
            mid=(l+r)/2;
            mmid=(mid+r)/2;
            if(f(mid)<f(mmid)) r=mmid;
            else l=mid;
        }
        printf("%.4f\n",f(mid));
    }
    return 0;
}

Error Curves(2010成都现场赛题)

时间: 2024-08-03 13:50:08

Error Curves(2010成都现场赛题)的相关文章

Go Deeper(2010成都现场赛题)(2-sat)

G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Here is a procedure's pseudocode: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep +

hdu 4089 不错的DP 北京现场赛题

http://acm.hdu.edu.cn/showproblem.php?pid=4089 还有疑惑,需要重新推: 但是学到的: 1.A=a+b+c  abc是三种情况,那么P(A)=a*P(a->事件)+b*P(b->事件)+c*P(c->事件); a->事件意思是 在a情况下的事件,就是全概率公式的思想吧 2.一定注意每一步会不会出现分母为0 的情况,以及预处理的时候对于一些特殊情况导致自己的式子会出现分母为0的排除掉 3.概率DP经常出现推出了式子但是自己不会写代码的情况,

hdu 4465 Candy 2012 成都现场赛

1 /** 2 对于大数的很好的应用,,缩小放大,,保持精度 3 **/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 #include <cstdio> 8 using namespace std; 9 10 int main() 11 { 12 double n,p; 13 int cnt =1; 14 while(cin>>n>>p){ 15

HDU-4464-Browsing History (2012 ACM/ICPC成都现场赛!)

Browsing History Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3065    Accepted Submission(s): 1692 Problem Description One day when you are going to clear all your browsing history, you come

hdu 5074 DP 2014鞍山现场赛题

hdu 5074 http://acm.hdu.edu.cn/showproblem.php?pid=5074 挺水的DP,注意依a[i-1]和a[i]的正负区分状态转移,然后O(n^3)即可轻易解决,我DP挺弱的也能过,貌似也就CF C题水平 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algo

hdu 4810 思维题+二进制位规律+异或规律 213南京现场赛题

http://acm.hdu.edu.cn/showproblem.php?pid=4810 以前做过一些涉及异或的题,化为二进制形式,然后统计0,1个数是一种很常见的处理方法,但是在做这个题的时候居然没尝试,脑残啊...... 一开始看5s时限,感觉稍微暴力一点应该可以,于是YY的O(n^3)算法但是没去实现,明显超时啊,大致就是通过C(n,1)的组合可以在O(n^2)内处理出C(n,2)的组合,在通过C(n,2)处理出C(n,3)的组合....但是C(n,2)已经是n^2个数了,所以算法是O

HDU-4472-Count (2012 ACM/ICPC成都现场赛)

Count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1850    Accepted Submission(s): 1200 Problem Description Prof. Tigris is the head of an archaeological team who is currently in charge of a

2012 ICPC/ACM 成都现场赛 Candy

题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=4465 题意:输入m,p;从两个盒子里各有n颗糖,每天取一颗,发现其中一个盒子空,求另外一个盒子糖果数的期望.p,1-p为取糖概率: 分析: 给盒子编号 A,B , 设另外一个盒子(为B) 剩  n - k 个 , 则 在过去的 (n + k)次 有 k 次 取到B , 且当前这次取到A . 同理 对另外一个盒子为(A) . 则 期望的公式为: Σ( n - k) * C(n + k , k) *

hdu 5073 2014鞍山现场赛题 物理题

http://acm.hdu.edu.cn/showproblem.php?pid=5073 推公式即可,质心公式segma(xi*wi)/segma(wi) 最终剩下的一定是连续n-k个星 然后枚举左边需要移除几个星即可 计算I的时候展开来算 比较坑的地方在于,星星的位置如果是int型,一定记得Double计算的时候 *1.0或者直接将位置数组声明为double  否则WA到死... //#pragma comment(linker, "/STACK:102400000,102400000&q