Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water

Educational Codeforces Round 88 (Rated for Div. 2)C. Mixing Water

题意:
热水温度h,冷水温度c,目标温度t
先热后冷,两种水来回倒,水的温度不断中和,使得杯子中水的温度与目标温度的差值最小。问最少需要多少杯水。

题解:
第一种:h=t,只需1杯
第二种:(h+c)/2≥t,这时只需2杯
第三种:先假设到n+1杯热水,n杯冷水,
此时的温度:t ≥ ((n+1)h+nc)/(2n+1)
化简可得:t ≥ (n
(h+c)+h)/(2n+1)
即: (t-h)/(h+c-2
t) ≥ n
只需要比较一下比较一下比t大和比t小的两个点即可,也就是比较一下n和n+1的情况
需要注意的是精度问题,记得开long double,我开double时wa2,脑子一下没转过来,卡着,掉了大分

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define _for(i,a,b) for(int i = (a);i<(b);i++)
#define endl  '\n'
using namespace std;
int main()
{
 	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T;cin>>T;
	while(T--)
	{
		ll h,c,t;
		cin>>h>>c>>t;
		if(h==t)cout<<1<<endl;
		else if(h+c>=2*t)cout<<2<<endl;
		else{
			int n1=((t-h)/(h+c-2*t));
			int n2=n1+1;
			long double m1=((n1+1)*h+n1*c)*1.0/(2*n1+1)*1.0;
			long double m2=((n2+1)*h+n2*c)*1.0/(2*n2+1)*1.0;
			if(fabs(m1-t)>fabs(m2-t)){
				cout<<2*n2+1<<endl;
			}
			else {
				cout<<2*n1+1<<endl;
			}			
		}
	}		 	 	
}

猜你喜欢

转载自blog.csdn.net/hrd535523596/article/details/106457847