题目大意:
有无限多的热水和无限多的冷水,给出他们的温度,按照热水-冷水-热水-冷水……的顺序往一个桶内倒水,桶内温度为倒入的热水和冷水的温度的平均值,询问倒入最少多少杯水能最接近给出的温度t。
(推式子的方法应该更简单…可惜本菜鸡不会)
二分思路:
1.可以发现,如果倒入的是偶数杯,即2,4,6,8……杯水,那么桶内温度一定固定为(h+c)/2。
2.因为是先倒热水,所以如果(h+c)/2>=t,那么之后再倒水一定不会比(h+c)/2更小了,所以倒两杯后(最小杯数)就是最接近t的温度。
3.除去1和2特判和h==t的情况,那么一定是奇数杯的情况,即热水比冷水多一杯,所以二分一下最接近的温度倒入的杯数,这里是二分出的右边界,所以要判断一下比右边界少2杯的时候,哪一个离t最接近。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int main(){
int _;
scanf("%d",&_);
while(_--){
int h,c,t;
cin>>h>>c>>t;
if(t*2<=h+c)cout<<2<<endl;
else if(t==h)cout<<1<<endl;
else {
ll tp;
ll l=1,r=5000000,temp=0;
while(l<=r){
ll mid=l+r>>1;
ll now=1+2*mid;
tp=(h+c)*mid+h;
if(tp%now==0&&tp/now==t){
temp=mid;
break;
}
double u=tp,d=now;
double res=u/d;
if(res<t){
temp=mid;
r=mid-1;
}
else l=mid+1;
}
ll now=1+temp*2;
tp=temp*(h+c)+h;
if(tp%now==0&&tp/now==t)cout<<now<<endl;
else {
ll now2=now-2;
ll tp2=tp-h-c;
double res1=t-tp/(1.0*now);
double res2=tp2/(1.0*now2)-t;
if(res2<=res1)cout<<now2<<endl;
else cout<<now<<endl;
}
}
}
return 0;}