Codeforces Round #834 (Div. 3) C. Thermostat

 题意概述:

丁同学家里有一个调温器,他的调温范围为(l,r)但是这个调温器有毛病,如果你想要从a调温到b

那么你必须满足abs(a-b)>=x。让你给出从a到b最少需要几步.

分析:如果a与b的距离小于x的话,那么我们只需要让温度去移动到距离当前温度最远的温度就可以了。以下是代码

#include<iostream>
#include<string>
#include<cmath>
#include<set>
#include <vector>
#include <algorithm>
using  namespace std;
bool is_in(int a,int b,int x){
    if(a<=x && x<=b){
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cout.tie(nullptr),cin.tie(nullptr);
    int t;cin>>t;
    while(t--){
        int l,r,x,a,b;
        set<int> vaild;
        cin>>l>>r>>x>>a>>b;
        int dist = abs(a-b);
        if(a==b){
            cout<<0<<endl;
            continue;
        }
        int res =0;
        if(dist>=x){
            cout<<1<<endl;
            continue;
        }
        bool no = false;
        while(dist<x && (a+x<=r || a-x>=l) ){
            if(vaild.find(a) != vaild.end()){
                res = 0;
                break;
            }
            vaild.insert(a);
            int k =0,s=0;
            bool ks = false,ss = false;
            if(abs(r-a)>=x)k = r,ks = true;
            if(abs(l-a)>=x)s = l,ss = true;
            int distk =0,dists = 0;
            if(ks){
                distk = abs(k-b);
            }
            if(ss){
                dists = abs(s-b);
            }
            if(dists>distk){
                a = s;
            }else{
                a = k;
            }
            dist = max(dists,distk);
            res++;
        }
        if(res==0){
            cout<<-1<<endl;
            continue;
        }
        cout<<res+1<<endl;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值