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);
}