您的位置:首页 > 其它

codeforces 936A Save Energy(数学思维)

2018-02-26 18:21 621 查看
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 .

题意: 要用微波炉烤一个面包,已知此微波炉开一次能烤k分钟,而每过d分钟,jury都会检查一次微波炉是否在工作,如果在工作则什么都不做,如果不在工作则把它打开。已知整个面包在微波炉工作的时候需要t分钟才能烤熟,若微波炉不工作的时候,依靠余温需要2t分钟才能烤熟,问总共需要多少分钟可以烤熟。(初始微波炉是开着的,并且只要一熟就可以拿出来)。

解法: 这道题主要要想到周期,微波炉开和关组成一个周期,然后只需计算出一个周期可以把面包烤成几成熟(这里几成熟可以用时间替代),需要经过几个周期,最后只需特殊处理要最后一个不完整的周期即可。

周期 T = ((k-1)/d+1)*d , 这里 (k-1)/d+1 代表向上取整。

例如 k = 3 d = 5 , T = 5 。 k = 5 d = 3 , T = 6 。

注意整形和浮点型的转换!!! 以及取整的细节

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
#define fre freopen("/Users/user/Desktop/in.txt","r",stdin);
#define ofre freopen("/Users/user/Desktop/out.txt","w",stdout);
#define CLR(s) memset(s,0,sizeof(s));
#define all(x) x.begin(), x.end()
typedef long long ll;

int main(){
ll k,d,t;

4000
cin >> k >> d >> t;
ll T = ((k-1)/d+1)*d;
double ans = 0;
double oneT = (T-k)*0.5+k; // 1个周期可以烤成 oneT
ans += ll(t/oneT)*T;
ll ht = t/oneT;
double st = t-ht*oneT; // 剩余还需 st
if(st < k)
ans += st;
else{
ans = ans+k+(st-k)*2;
}
printf("%.1lf\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: