三分算法+HDU - 3714 Error Curves

 

目录

1:二分三分的区别:

 

2:三分算法:

 

3:算法模板:

 

4:算法的正确性分析:

 


 

1:二分三分的区别:

二分法适用于求单调的时候用的,就比如说排序好的数组,那是递增的或者递减的,三分主要用来解决单峰函数,单峰函数使用二分来做相对于三分来说并不容易,虽然我们在某些情况下可以通过求导得到单调的导函数。

 

2:三分算法:

 

 

 

如上图,我们定义了L和R,mid = (L + R) / 2,midmid = (mid + R) / 2;我们把整个区间分成了三份,也就是二分的二分。

类比二分的思想,我们在定义两个点之后,要不断对这两个点之间的关系进行判断,使得我们所要求的值所在的区间不断缩小。

接下来就来谈谈怎么不断缩小这个区间

 

我们知道图中点的横坐标的话,就可以通过函数求出函数的纵坐标,假如说现在要求最大值,那么根据途中mid点纵坐标大于midmid点的纵坐标这个信息可知:最大值的区间可以由[L,R]缩减为[L,midmid],当说到这里的时候,估计很多人会问:要是mid不在轴线的左侧,而是在右侧还成立吗?当然,我们每一步所要做的就是不断逼近预期值,就算是两个点都在对称轴的一侧最大值依旧距离纵轴比较大的那个点较近,所以此时也是成立的。

 

    while(right-left>dou)
    {
        mid=(left+right)/2;
        midmid=(mid+right)/2;
        mid_value=calcc(mid);
        midmid_value=calcc(midmid);
        if(mid_value>=midmid_value)
            right=midmid;
        else
            left=mid;
    }  

这就是整个算法的核心部分,那我问题就来了,我提出四个问题:

 

1:如果说抛物线开口向上:

        1:最大值是多少怎么求?

        2:最小值是多少怎么求?

2:如果说抛物线开口向下:

       1:最大值是多少怎么求?

       2:最小值是多少怎么求?

 

不管是抛物线开口向上还是抛物线开口向下,欲求最小值,我们只需在每次循环的时候逼近最小值就行了,欲求最大值,只需要在每次循环的时候逼近最大值就行了(因为抛物线是对称的)。

 

 

3:算法模板:

const double du=1e-9;

double calcc(type num)
{
    //根据题目意思计算
}

double sanfen()
{
    double left,right;
    double mid,midmid;
    double mid_value,midmid_value;
    while(right-left>dou)
    {
        mid=(left+right)/2;
        midmid=(mid+right)/2;
        mid_value=calcc(mid);
        midmid_value=calcc(midmid);
        if(mid_value>=midmid_value)
            right=midmid;
        else
            left=mid;
    }    
}

 

4:算法的正确性分析:

1、mid与midmid在最值的同一侧。由于凸性函数在最大值(最小值)任意一侧都具有单调性,因此,mid与midmid中,更大(小)的那个 数自然更为靠近最值。此时,我们远离最值的那个区间不可能包含最值,因此可以舍弃。

2、mid与midmid在最值的两侧。由于最值在中间的一个区间,因此我们舍弃一个区间后,并不会影响到最值

 

 

HDU - 3714  Error Curves 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8333    Accepted Submission(s): 3132
 

Problem 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 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.

Sample Input

2

1

2 0 0

2

2 0 0

2

-4

2

Sample Output

0.0000

0.5000

 

题意:

 

给出n个二次函数,定义域为[0,1000], 求x在定义域中每个x所在的n个函数的最大值的最小值

 

题解:

 

求每个x所在的n个函数的最大值的最小值,我们可以先求出在[0,1000]范围内的某个位置的函数的两个最大值,比较这个两个值的最小值,不断缩减空间范围,最后求解。

 

C++AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<math.h>
typedef  long long LL;
int INF=0x3f3f3f3f;
double dou=1e-15;//精度问题
double dou2=1e-6;
using namespace std;
const int maxn=1e4+200;
double a[maxn],b[maxn],c[maxn];
int n;
double calcc(double num)
{
    double ans=a[0]*num*num+b[0]*num+c[0];
    for(int i=1;i<n;++i)
    {
        ans=max(ans,a[i]*num*num+b[i]*num+c[i]);
    }
    return ans;
}

double sanfen(void)
{
    double left,right;
    double mid,midmid;
    double mid_value,midmid_value;
    left=0.0,right=1000.0;
    while(right-left>dou)
    {
        mid=(left+right)/2;
        midmid=(mid+right)/2;
        mid_value=calcc(mid);
        midmid_value=calcc(midmid);
        if(mid_value<midmid_value)
            right=midmid;
        else
            left=mid;
    }
    return mid_value;
}

int main()
{
    ios::sync_with_stdio(false);
    int N;
    cin>>N;
    while(N--)
    {
        cin>>n;
        for(int i=0;i<n;++i)
        {
            cin>>a[i]>>b[i]>>c[i];
        }
        printf("%.4lf\n",sanfen());
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值