D - Freefall
https://atcoder.jp/contests/abc279/tasks/abc279_d
思路
求凹函数的极小值
https://www.cnblogs.com/luoyj/p/12408277.html#6
#include<bits/stdc++.h> using namespace std; const double eps = 1e-6; int n; double a[15]; double f(double x){ //计算函数值 double s=0; for(int i=n;i>=0;i--) //注意函数求值的写法 s = s*x + a[i]; return s; } int main(){ double L,R; scanf("%d%lf%lf",&n,&L,&R); for(int i=n;i>=0;i--) scanf("%lf",&a[i]); while(R-L > eps){ // for(int i = 0; i<100; i++){ //用for也行 double k =(R-L)/3.0; double mid1 = L+k, mid2 = R-k; if(f(mid1) > f(mid2)) R = mid2; else L = mid1; } printf("%.5f\n",L); return 0; }
Code
https://atcoder.jp/contests/abc279/submissions/36830825
void solve() { // double EPS = 0.0000001; cout << fixed << setprecision(10); double a, b; cin >> a >> b; auto cal = [&](int x) -> double { return x * b + a / sqrt(1+x); }; // cout << fixed << setprecision(10); // int x = pow(a/2/b, 2/3) - 1; // cout << min({cal(x-1), cal(x), cal(x+1)}) << endl; int left = 0, right = INF; while(right - left > 2) { int mid1 = (left * 2 + right) / 3; int mid2 = (left + right * 2) / 3; double t1 = cal(mid1); double t2 = cal(mid2); if(t1 <= t2) { right = mid2; }else { left = mid1; } } double ans = a; repi(i, max(0LL, left-10), right+10) chmin(ans, cal(i)); cout << ans << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }
标签:Freefall,ATCODER,--,double,int,https,mid2 From: https://www.cnblogs.com/lightsong/p/16930906.html