C. Mixing Water(三分)

题:https://codeforces.com/contest/1359/problem/C

题意:对于一个容器,每次能往里面倒一瓶水,热水和冷水交替倒,最少倒入一瓶水,容器水的温度等于总体的平均温度,给定温度 t ,问最少倒几瓶水最终容器中水的温度能最接近温度t  

分析:这个问题可以二分三分解,关键看你怎么写check函数,这里只讲三分,对于倒入的杯数x关于温度T的函数的递减的,那么t-T函数就是一个抛物线,所以用三分解决。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e6+6;
const ll inf=0x3f3f3f3f;
int h,c,t;
double check(ll x,ll y){
    return fabs((double)t-1.0*(x*h+y*c)/(x+y));
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--){
 
        cin>>h>>c>>t;
        if(h+c>=2*t){
            cout<<2<<endl;
        }
        else{
            ll l=0,r=inf,lmid,rmid;
            while(l<r){
                lmid=l+((r-l)/3);
                rmid=r-((r-l)/3);
                if(check(lmid+1,lmid)<=check(rmid+1,rmid))
                    r=rmid-1;
                else
                    l=lmid+1;
            }cout<<2*l+1<<endl;
        }
 
    }
    return 0;
}
View Code

 

posted @ 2020-05-30 20:08  starve_to_death  阅读(197)  评论(0编辑  收藏  举报