CodeForces - 1359C Mixing Water

There are two infinite sources of water:

hot water of temperature h;
cold water of temperature c (c<h).
You perform the following procedure of alternating moves:

take one cup of the hot water and pour it into an infinitely deep barrel;
take one cup of the cold water and pour it into an infinitely deep barrel;
take one cup of the hot water …
and so on …
Note that you always start with the cup of hot water.

The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.

You want to achieve a temperature as close as possible to t. So if the temperature in the barrel is tb, then the absolute difference of tb and t (|tb−t|) should be as small as possible.

How many cups should you pour into the barrel, so that the temperature in it is as close as possible to t? If there are multiple answers with the minimum absolute difference, then print the smallest of them.

Input
The first line contains a single integer T (1≤T≤3⋅104) — the number of testcases.

Each of the next T lines contains three integers h, c and t (1≤c<h≤106; c≤t≤h) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.

Output
For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to t.

Example
Input
3
30 10 20
41 15 30
18 13 18
Output
2
7
1
Note
In the first testcase the temperature after 2 poured cups: 1 hot and 1 cold is exactly 20. So that is the closest we can achieve.

In the second testcase the temperature after 7 poured cups: 4 hot and 3 cold is about 29.857. Pouring more water won’t get us closer to t than that.

In the third testcase the temperature after 1 poured cup: 1 hot is 18. That’s exactly equal to t.

题目分析:
现在越是越来越懒了,根本不想写题目大意,大家自行百度吧
对于冷水和热水倒入的杯数一样多的时候显然是没有什么用的,和各自倒入一杯一样,而且发现热水永远比冷水多,所以最低温度一定是(h+c)/2 ,当t<(h+c)/2 的时候 直接输出2
·对于别的情况我们设热水到了n杯,那么冷水就是n-1杯 此时温度的函数是h*n + c * (n - 1) / (2 * n -1) 导数可得该函数单调递减,二分答案解,最后还需要判断一下需要左端的点还是右端的点,最气人的是这个题 十分钟出正解思路,但是乘法爆long long卡了我一下午

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
typedef long long LL;
// 感觉像是二分答案  但是苦于绝对值 形成一个单峰函数   不适用于二分答案
// 但是温度函数f(t) = (h * n + c * n - c)/(2n - 1)   可以由导数证明  该函数单调  故二分答案
// 对于问题的分析好像没有问题  **的是我的求导函数居然错了 作者是一个单调递减的函数  之前居然做了一个单调递增
using namespace std;
// *B*   这样写的代码思路没有问题   之前一直苦于寻找错误   没想到被卡long long
int main(int argc,char *argv[]) {
    int T,c,h,t,n; scanf("%d",&T);
    while(T--) {
        scanf("%d %d %d",&h,&c,&t);
        if(h + c >=  2 * t) { cout << 2 << endl; continue; }
        int l = 1,r = 1000000,Mid,Ans;
        while(l <= r) {
            Mid = l + r >> 1;
            double tempu = 1LL * Mid * h + 1LL * Mid * c - c;
            tempu /= (2.0 * Mid - 1.0);
            if(tempu >= 1.0 * t) Ans = Mid, l = Mid + 1;
            else r = Mid - 1;
        }
        double t1 = 1LL * Ans * h + 1LL * Ans * c - c;
        double t2 = 1LL * (Ans + 1) * h + 1LL * (Ans + 1) * c - c;

        t1 /= 1.0 * (2 * Ans - 1); t2 /= 1.0 * (2 * Ans + 1);
        if(fabs(t1 - t * 1.0) > fabs(t2 - t * 1.0)) cout << 2 * Ans + 1 << endl;
        else cout << 2 * Ans - 1 << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值