Mixing Water CodeForces - 1359C(规律+三分)

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.
思路:
根据题意,我们在罗列杯子的个数的时候可以发现:2,4,6,8,…的时候,温度是一样的,但是呢,1,3,5,7的时候,温度是逐渐降低的。也就说,单数的时候,温度是逐渐递减的。和t的差的绝对值就是一个开口向上的函数,这样的话,就可以用三分法来解决。三分枚举的是第i个奇数。最后的结果和2的时候比较(因为偶数都是一样的,杯数最少就是2了),选取最优的。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int h,c,t;

inline double cal(ll x)
{
	int num=x*2-1;
	double tem=(double)(h*(x)+c*(x-1))/(double)num;
	return fabs(tem-t);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&h,&c,&t);
		if(h==t) cout<<1<<endl;
		else 
		{
			int l=1,r=1e7;
			while((r-l)>=3)
			{
				ll lmid=l+(r-l)/3;
				ll rmid=r-(r-l)/3;
				if(cal(lmid)>cal(rmid)) l=lmid;
				else r=rmid;
			}
			double ans=1e10;ll pos;
			for(ll i=l;i<=r;i++) 
			{
				double tem=cal(i);
				if(ans>tem)
				{
					ans=min(ans,cal(i));
					pos=i;
				}
			}
			double tem=fabs((double)(h+c)/2.0-t);
			if(tem<ans) cout<<2<<endl;
			else cout<<pos*2-1<<endl;
		}
	}
	return 0;
}

(ps:这道题目二分也能做,而且比三分简单不少好像)。
努力加油a啊,(o)/~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值