codeforces-936A-Save Energy!

Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.

During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.

It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.

Input
The single line contains three integers k, d and t (1 ≤ k, d, t ≤ 1018).

Output
Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9.

Namely, let’s assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if .

Examples
inputCopy
3 2 6
output
6.5
inputCopy
4 2 20
output
20.0
Note
In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready .

In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes.

这道题主要是要找到周期。
代码如下

#include <cstdio>
typedef long long ll;
int main()
{
    ll k,d,t,T;
    double ans= 0,st;
    scanf("%lld%lld%lld",&k,&d,&t);
    T=((k-1)/d+1)*d;//周期长度
    double T1 = k+(T-k)*0.5;//实际周期长度
    ll num = t/T1;//周期数
    ans=num*T;
    st = t - ll(t/T1)*T1;
    if(st<k)
        ans = ans+st;
    else
        ans = ans+k+(st-k)*2;
    printf("%.1lf",ans);
    return 0;
}

二分(查找最优时间):

#include <cstdio>
typedef long long ll;
ll k,d,t,T;
bool findl(double x)
{
    ll p = (ll)x/T;//周期数
    double aa = p*k;//周期内烹饪时间
    double bb = p*(T-k);//周期内保温时间
    x -=p*T;//剩余时间
    if(x<=k)
        aa+=x;
    else
        aa+=k,bb=bb+x-k;
    return (2*aa+bb>=2*t);//判断是否大于2t
}

int main()
{
    scanf("%lld%lld%lld",&k,&d,&t);
    T=((k-1)/d+1)*d;//周期
    double l = 0.0,r=3e18;
    for(int a = 1; a <= 100; a ++)
    {
        double mid = (l+r)/2;
        if(findl(mid))
            r=mid;
        else
            l=mid;
    }
    printf("%.1lf",l);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值