C. 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 4 6 8 这种偶数次的话,水温是同一个温度,然后是奇数次的话,随着次数的增加平均水温不断减少,并趋近于最低温度。
如图所示
在这里插入图片描述
这样,我们就能知道

1, 如果给的温度t是大于最高温度h的话,那么就只需要放一次热水,温度为

t,此时的温度相差最小。

2 如果给的温度t是小于最低温度(h+c)/2的话,那么就只需要放一次热水和

一次冷水,温度为(h+c)/2,此时的温度相差最小。

3 如果我们给的温度是在这个区间里面,该怎么求解呢,我们首先想到依次

遍历所有奇数次,但是这样明显会超时,所以还是要从公式入手

可以得到,如果操作次数为偶数次,则平均水温一定为 mid

但此时答案操作次数为奇数次,令答案为 ans

则操作 ans-1 次时,得到水温总和为 (ans-1)*mid

第 ans 次加入的是热水 h

所以可以得到如下公式
在这里插入图片描述
变换一下,得到
在这里插入图片描述
所以可以得到答案的近似操作次数

由于操作次数一定是个整数,所以可以在求出此时的 ans 后,在 ans 周围枚举判断下哪一次操作才是真正的答案

计算误差不会太大,所以在 ±3 范围内查找即可

ac代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+100;
const int inf=0x6fffff;
double T,h,c,t;
char a[maxn][maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>h>>c>>t;
        double mid=(h+c)/2;
        if(t>=h)
        {
            cout<<1<<endl;
            continue;
        }
         if(t<=mid)
        {
            cout<<2<<endl;
            continue;
        }
       
        int d=(mid-h)/(mid-t),ans;
        int i=max(1,d-3);

        double mx=1e8;
        if(i%2==0)i--;
        for(;i<=d+3;i+=2)
        {
            if(fabs(1.0*((i-1)*mid+h)/i-t)<mx)
            {
              mx=fabs((1.0*(i-1)*mid+h)/i-t);
              ans=i;
            }
        }
        cout<<ans<<endl;
        
        
    }
    //system("pause");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值