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;
			}			
		}
	}		 	 	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章