Codeforces Round #834 (Div. 3) C. Thermostat
First let’s consider the cases when the answer exists:
- If a = b a=b a=b, then the thermostat is already set up and the answer is 0 0 0.
- else if ∣ a − b ∣ ≥ x |a−b|≥x ∣a−b∣≥x, then it is enough to reconfigure the thermostat in 1 1 1 operation.
- else if exist such temperature c c c, that ∣ a − c ∣ ≥ x |a−c|≥x ∣a−c∣≥x and ∣ b − c ∣ ≥ x |b−c|≥x ∣b−c∣≥x, then you can configure the thermostat in 2 2 2 operations. If such c c c exists between l l l and r r r, we can chose one of bounds: a → l → b a→l→b a→l→b or a → r → b a→r→b a→r→b.
If we can’t get the temperature b b b in one of these ways, the answer is − 1 −1 −1.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;cin>>T;
while(T--)
{
int l,r,x;cin>>l>>r>>x;
int a,b;cin>>a>>b;
if(a==b) cout<<"0"<<endl;
else
{
int L=b-x;
int R=b+x;
if(a<=L || a>=R) cout<<"1"<<endl;
else
{
if(a>b && a<R)
{
if(b-l>=x) cout<<"2"<<endl;
else if(r-a>=x) cout<<"2"<<endl;
else if(a-l>=x && r-b>=x) cout<<"3"<<endl;
else cout<<"-1"<<endl;
}
if(a>L && a<b)
{
if(r-b>=x) cout<<"2"<<endl;
else if(a-l>=x) cout<<"2"<<endl;
else if(r-a>=x && b-l>=x) cout<<"3"<<endl;
else cout<<"-1"<<endl;
}
}
}
}
return 0;
}