三分法-洛谷P3382

wampserver没下下来,lcm莫比乌斯反演写挂了,splay树段错误, nobody ever knows writing bugs better than me

然后今晚要打cf,不如先写个三分法吧,老是出,求函数凸包用的

模板:

#include <bits/stdc++.h>
using namespace std;
#define limit (10000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
}//快读
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
double coefficient[limit];
double f(double x, int n){
    double ans = 0;
    per(i,1,n){
        ans += coefficient[i] * pow(x, i);
    }
    return ans;
}
int n;
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    double l,r;
    FASTIO
    cin>>n>>l>>r;
    per(i,1,n){
        cin>>coefficient[i];
    }
    while (r - l >= EPS){
        double mid = l + (r - l) / 2;
        double tid = mid + (r - mid) / 2;
        if(f(mid,n) >= f(tid, n) ){
            r = tid;
        }else{
            l = mid;
        }
    }
    cout<<fixed<<setprecision(5)<<(l + (r -l) / 2)<<endl;
    return 0;
}
AC Code

 

posted @ 2020-08-25 14:50  tiany7  阅读(122)  评论(0编辑  收藏  举报