Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water[题解](数学)

C. Mixing Water

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
30 10 20
41 15 30
18 13 18
outputCopy
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.
题目的大概意思就是一杯热水,一杯冷水这样重复的加入到一个无限大的容器中,问总共需要多少杯水才能使温度最接近给定的温度。
首先来看,暴力会T 热水与冷水1:1温度当然是(c+h)/2啦,而题目要求又是先加热水,后加冷水,所以该容器温度T在[(c+h)/2,h]之间,如果给定温度小于(c+h)/2,那么不用考虑,直接是2杯~下面列举了T在两者之间的情况的公式求解
在这里插入图片描述
有了公式之后,运用公式能够求出需要多少杯热水,然后将其左右值都算一遍看看谁更接近T,因为一些细节上的原因,WA了两发。。。
接下来上代码:

#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        double h,c,t;
        scanf("%lf%lf%lf",&h,&c,&t);
        double k=(h+c)/2.0;
        if(t<=k)printf("2\n");
        else {
            double s=(c-t)/(h+c-2*t);
        double a1=ceil(s),a2=floor(s),j=a1;
        double x=(a1*h+(a1-1)*c)/(a1+a1-1),y=(a2*h+(a2-1)*c)/(a2+a2-1);
        if(abs(x-t)>=abs(y-t)){j=a2; }
            printf("%.0lf\n",j+j-1);
        }
 
    }
    return 0;
}

代码还算简单,听说二分也可以过。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值