1359C 1700
题意:给你三个数,h,c,t,分别为热水温度,冷水温度,和预期想得到的温度,其中满足c<=t<=h,先对于一个空瓶进行操作,且只能先倒入热水,再倒入冷水,再倒入热水,依次进行下去,加入热水的数量只能等于冷水的数量或者等于冷水的数量加一,问倒入最少的杯数最接近预期温度。
思路:分类讨论
当热水杯数=冷水杯数时,温度一直为(h+c)/2,所以最少的杯数 = 2,
当热水杯数=冷水杯数+1时,分类讨论,首先要知道不断加入热水,最终的温度是不断向(h+c)/2靠近的,而且还是单调递减的向(h+c)/2靠近。
所以如果t * 2<h+c时,也就是说最少杯数=2,这个可以画个图理解一下,可以假设(h+c)/2是一条渐进线,这个函数一直在递减的向这个渐近线靠近,但预期温度是在这个渐近线下方的,所以最优的杯数=2。
如果t * 2>h+c时,对倒入热水的杯数进行二分处理,当然也可以用规律得到复杂度为O(1),二分为O(logn),最终找到最接近预期温度的杯数,应该说是比预期温度大但差距最小的杯数,所以还要用得到的杯数L和L-1相比较,得到最后答案。
注意因为这个算式涉及到精确度的问题,所以最好就是用整数之间的比较
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define per(i, a, n) for(int i = n-1; i >= a; i--)
#define INF 1ll<<60
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const ll maxn = 4e6+11;
int main(){
IOS;
int t;
cin>>t;
while(t--){
ll h, c, t;
cin>>h>>c>>t;
if(t*2 <= (h+c)){
cout<<"2"<<endl;
continue;
}
int l = 0, r = maxn;
while(l<=r){
ll mid = (l+r)/2;
if(mid*(c+h)+h < t*(mid*2+1)){
r = mid-1;
}else l = mid+1;
}
ll s1 = l*2+1, s2 = (l-1)*2+1;
if(s2*s1*t-(l*(h+c)+h)*s2 < ((l-1)*(h+c)+h)*s1-t*s1*s2) cout<<l*2+1<<endl;
else cout<<(l-1)*2+1<<endl;
}
return 0;
}