Codeforces #467 (Div. 2) C. Save Energy! ( 数学

本文介绍了一个节能烹饪场景下的算法问题:给定电磁炉开关周期、检查间隔及烹饪所需时间,求解将食物均匀煮熟所需的总时间。通过数学判定结合二分查找的方法找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. 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.

输入

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

输出

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 .

样例

input
3 2 6
output
6.5
input
4 2 20
output
20.0

题意

一个电磁炉烹饪一只鸡 打开时候需要t时间 否怎需要2t时间
一次点火可以持续k时间 每经过d时间就检查一下电磁炉是否打开 问这只鸡煮熟的总时间

数学判定+二分查找最优时间

AC代码

#include <bits/stdc++.h>
using namespace std;

#define CLR(a,b) memset(a,(b),sizeof(a))
#define LL long long

const int MAXN = 1e3+10;
const double eps = 1e-15;
LL k, d, t, ans;
LL aa, bb;

bool check(double x)
{
    double a, b;
    LL p = (LL)x/ans;
    a = aa * p;
    b = bb * p;
    x -= ans * p;
    if(x <= aa)
        a += x;
    else
        a += aa, b += x-aa;
    return (a*2+b>=t*2);
}

int main()
{
    cin >> k >> d >> t;
    ans = d*((k+d-1)/d);
    aa = (k/d)*d+(k%d);
    bb = ans - aa;
    double l = 0.0, r = 3e18;
    for(int i = 1; i <= 100; i++) {
        double id = (l+r)/2;
        if(check(id))
            r = id;
        else
            l = id;
    }
    return 0*printf("%.15f\n",l);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值