【GDOI 2016 Day1】中学生数学题

Description

Solution

很明显这是一个单峰函数嘛~
第一问直接用三分来搞,
第二问用一个三分套三分即可。
或者用公式求也可以,
因为求的是 n0kp(pp0) ,
(n0kp) 非常接近一个整数时,ans才可能最优,
所以可以拆开: kP2+kp0p+n0pn0p0 ;
用二次函数顶点式即可,
第二问三分,

这题出成中考题一定很爽。——crazy_czy

Code

Code from liy_7_29,

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double db;
int main()
{
    freopen("math.in","r",stdin);
    freopen("math.out","w",stdout);
    db n0,p0,k,t=0;
    scanf("%lf %lf %lf",&n0,&p0,&k);
    db a=-1/k,b=n0/k+p0,c=-n0*p0;
    db x=-b/(2*a);
    db ans=(n0-round(x))*(round(x)/k-p0);
    printf("%.3lf ",ans);
    ans=0;
    for(db x1=0;x1<=n0;x1++)
    {
        a=-1/k,b=x1/k+p0,c=n0*x1/k-n0*p0-x1*x1/k;
        db x2=-b/(2*a),y=(n0-x1)*(x1/k-p0);
        t=(x1-round(x2))*(round(x2)/k-p0)+y;
        if(t>ans) ans=t;
    }
    printf("%.3lf",ans);
    fclose(stdin);fclose(stdout);
    return 0;
}

你可能感兴趣的:(函数,三分,GDOI,GDOI2016)