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;;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章