Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water(数学+二分法)---题解

C. Mixing Water
来源:http://codeforces.com/contest/1359/problem/C

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.

思路:

  • 很容易观察到,如果倒进去的水是偶数杯,那温度一定是 (h+c)/ 2 。而奇数杯的话 ,越多肯定是越接近于(h+c)/2,并且是递减的。
  • 然后考虑一下特殊情况,如果 h=t ,那答案一定是 1,如果 (h+c)/2 >=t ,那答案一定是 2,因为达到平均温度的时候 差值是最小的,最后的温度不可能低于平均温度。
  • 最后考虑普遍的情况就是 找最接近于温度 t 的左右两个状态,一个大于 t 一个小于 t ,二分奇数杯就好了。(有点像高中的数学题)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + 7;
using namespace std;
int q, h, c, t;
char s[MAXN][MAXN];
double f(ll mid) 
{
    return (mid * (h + c) + h) * 1.0 / (mid * 2 + 1) * 1.0;
}
int main() 
{
    cin >> q;
    while (q--) 
    {
        cin>>h>>c>>t;
        if (t >= h) 
        {
           cout<<1<<endl;
            continue;
        }
        if (h + c >= 2 * t) 
        {
           cout<<2<<endl;
            continue;
        }
        ll l = 0, r = 1e9, mid, ans = 0;
        while (l <= r) 
        {
            mid = (l + r) / 2;
            if (f(mid) >= t * 1.0) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        if (fabs(f(ans) - t * 1.0) > fabs(f(ans + 1) - t * 1.0))
           cout<<2 * ans + 3<<endl;
        else
            cout << 2 * ans + 1 << endl;;
    }
}

"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值