codeforces 1359C

1359C

题意

有两种水,高温为h,低温为c,数量不限,分别把他们导入一个无限深的桶中,试问倒多少杯水的时候平均温度最接近t,必须是先h后c

分析

1.当h==t的时候,res=1
2.h+c==t*2的时候,res=2
3.average>t*2,需要加c
4.average<t*2,需要加h
但是每次都是必须加h之后才能加c,所以第三种情况归类为第二种情况
每次多加h,这样求出的平均值是单调递减的,所以可以使用二分找到最接近t的一个平均值,然后把左右两边都判断一下取最小值

题解

#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define lowbit(x) x&(-x)
#define INF 0x7fffffff
#define eb emplace_back
#define pb push_back
#define divUp(a,b) (a+b-1)/b
#define mkp(x,y) make_pair(x,y)
#define lb lower_bound
#define ub upper_bound
#define int long long
using namespace std;
typedef unsigned long long ull;
typedef pair<int, int> pii;
int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);};
bool checkP2(int n) {return n > 0 and (n & (n - 1)) == 0;};
double h,c,t;
void solve(){
    cin>>h>>c>>t;
    if(h<=t) cout<<1<<endl;
    else if(h+c>=t*2) cout<<2<<endl;
    else{
        auto get=[](int mid){
            int cur=mid*2+1;
            double ret=(mid*(h+c)+h)/cur*1.0;
            return ret;
        };
        int l=0,r=1e9;
        while(l<r){
            int mid=(l+r+1)>>1;
            if(get(mid)>=t*1.0) l=mid;
            else r=mid-1;
        }
        if(abs(t*1.0-get(l))>abs(t*1.0-get(l+1))) cout<<2*(l+1)+1<<endl;
        else cout<<2*l+1<<endl;
    }
}
signed main(){
    IOS;
    int _;cin>>_;while(_--) solve();
    return 0;
}
posted @ 2021-09-30 01:02  指引盗寇入太行  阅读(45)  评论(0编辑  收藏  举报