Codeforces 1359 C. Mixing Water(二分)

题意:

在这里插入图片描述
一杯热水,一杯凉水,一杯热水,一杯凉水,一杯热水…
这样往杯子里加,找到最接近给定温度T,输出最小的加水次数
可以发现,加偶数次,即热水杯水等于凉水,它的温度是不变的始终是(h+c)/2
加奇数次,即热水杯水等于凉水+1,它的温度随着杯数增加是递减的
二分找到奇数杯的最接近温度T的,二分结果可能有误差,在上下摆动算一下最小的就行了


typedef long long ll;
using namespace std;
const int MAXN=1e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
//::iterator it;
inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
ll c,h,T;
double get(ll m){
    return (1.0*(m/2)*(h+c)+h)/m;
}
int main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int t;
    
    cin>>t;
    while(t--){
        cin>>h>>c>>T;
        if(h==T){
            cout<<1<<endl;
            continue;
        }
        double x=(h+c)/2.0;

        double st=(h*2+c)/3.0;
        ll l=1,r=1e10;
        ll m;
        while(l<r){
            m=(l+r)/2;
            if(m%2==0)m++;
            //cout<<l<<" "<<r<<" "<<m<<" "<<endl;
            if(get(m)<T)r=m-1;
            else l=m+1;
        }
        if(l%2==0)l++;
        //cout<<l<<endl;
        ll ans=l;
        double mi=get(l);
        for(ll i=max(1ll,l-2);i<=l+2;i+=2){//上下摆动找最小的
            //cout<<i<<" "<<get(i)<<" "<<fabs(get(i)-T)<<" "<<fabs(mi-T)<<endl;
            if(fabs(get(i)-T)<fabs(mi-T)){
                ans=i;
                mi=get(i);
            }
            if(fabs(get(i)-T)==fabs(mi-T)){
                ans=min(ans,i);
            }
        }
        if(fabs(mi-T)>=fabs(x-T))ans=2,mi=x;//x是两杯的温度
        cout<<ans<<endl;
    }
    return 0;
}
/*
1
41 15 30
1
656428 610660 653269
1
1138 4 643
*/

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值