题意概述:
丁同学家里有一个调温器,他的调温范围为(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;
}
}