(三分入门)(凹函数)

三分具体讲解:

https://blog.csdn.net/qq_40859951/article/details/79515299

 

题目链接:https://www.luogu.org/problemnew/show/P3382

题目大意:给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减。试求出x的值。

具体思路:三分入门题

AC代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cmath>
 4 #include<iomanip>
 5 using namespace std;
 6 # define ll long long
 7 const int maxn = 6e5+100;
 8 double a[maxn];
 9 int n;
10 double  qpow(double t1,int t2)
11 {
12     double ans=1.0;
13     while(t2)
14     {
15         if(t2&1)
16             ans=ans*t1;
17         t1=t1*t1;
18         t2>>=1;
19     }
20    // cout<<t1<<" "<<t2<<" "<<ans<<endl;
21     return ans;
22 }
23 double cal(double mid)
24 {
25     double ans=0;
26     for(int i=n; i>=0; i--)
27     {
28         ans+=a[i]*qpow(mid,i);
29     }
30     return ans;
31 }
32 double check(double l,double r)
33 {
34     double mid,midd;
35     while(r-l>=1e-15)
36     {
37         mid=(r+l)/2.0;
38         midd=(mid+r)/2.0;
39         if(cal(mid)<=cal(midd))
40             l=mid;
41         else
42             r=midd;
43        //     cout<<l<<" "<<r<<endl;
44     }
45    // cout<<123456<<" "<<r<<endl;
46     return r;
47 }
48 int main(){
49     double l,r;
50     scanf("%d %lf %lf",&n,&l,&r);
51     for(int i=n; i>=0; i--)
52     {
53         scanf("%lf",&a[i]);
54     }
55     double tmp=check(l,r);
56   //  cout<<fixed<<setprecision(5)<<tmp<<endl;
57     printf("%.2lf\n",tmp);
58     return 0;
59 }

 

posted @ 2019-03-19 16:47  Let_Life_Stop  阅读(583)  评论(0编辑  收藏  举报