题解:P13244 [GCJ 2014 Qualification] Cookie Clicker Alpha

· · 题解

P3244 题解:

主要思路:

从不买任何农场的情况开始,存储当前情况的时间,然后逐一枚举如果购买一所农场所需要的曲奇的生产时间,如果小于上一个情况,更新当前最优解。

代码实现:

定义一个变量 tz 存储答案,初始化为任何农场都不买的情况 X\div r,其中 r 定义为当前的生产速度,然后用 while 循环枚举如果购买当前农场所需曲奇的生产时间,如果小于 tz,更新 tz 的值。枚举完所有情况后,此时的 tz 存储的就是最优解,输出 tz 的值即可。

AC Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int T;
int32_t main(){
    cout.tie(nullptr)->ios::sync_with_stdio(false);
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin>>T;
    cout<<fixed<<setprecision(7);//保留七位小数。
    for(int cas=1;cas<=T;cas++){
        double c,f,x;
        cin>>c>>f>>x;
        double e=0.0;
        double r=2.0;
        double tz=x/r; 
        while(1){
            double tx=c/r;
            e+=tx;
            r+=f;
            double ty=e+x/r;
            if(ty<tz) tz=ty;//如果有更优解,更新当前值。
            else break;
        }
        cout<<"Case #"<<cas<<": "<<tz<<endl;//注意输出格式。
    }
}

~求过。~