题目描述:
Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of \(a\).
The thermostat can only be set to a temperature from \(l\) to \(r\) inclusive, the temperature cannot change by less than \(x\). Formally, in one operation you can reconfigure the thermostat from temperature \(a\) to temperature \(b\) if \(|a−b|≥x\) and \(l≤b≤r\).
You are given \(l\), \(r\), \(x\), \(a\) and \(b\). Find the minimum number of operations required to get temperature \(b\) from temperature \(a\), or say that it is impossible.
输入描述:
The first line of input data contains the single integer \(t\) (\(1≤t≤10^4\)) — the number of test cases in the test.
The descriptions of the test cases follow.
The first line of each case contains three integers \(l\), \(r\) and \(x\) (\(−10^9≤l≤r≤10^9\), \(1≤x≤10^9\)) — range of temperature and minimum temperature change.
The second line of each case contains two integers \(a\) and \(b\) (\(l≤a,b≤r\)) — the initial and final temperatures.
输出描述:
Output \(t\) numbers, each of which is the answer to the corresponding test case. If it is impossible to achieve the temperature \(b\), output \(-1\), otherwise output the minimum number of operations.
题解:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int T;
int l, r, x, a, b;
void solve()
{
cin >> l >> r >> x >> a >> b;
int minn = min(a, b); // 找出 a 和 b 中的最小值
int maxx = max(a, b);
if (a == b) // 如果 a 和 b 相等则不用移动
{
cout << 0 << '\n';
return;
}
if (abs(a - b) >= x) // 如果 a he b 的查大于等于 x 则直接将 a 移动到 b
{
cout << 1 << '\n';
return;
}
if (maxx + x <= r || minn - x >= l) // 若最大值加上 x 的值仍然在 r 的范围内或者最小值减去 x 的值仍然在 l 的范围内,那么就代表可以只用移动两次
{
cout << 2 << '\n';
return;
}
if (minn + x > b && minn + x <= r && maxx - x < a && maxx - x >= l) // 若最小值加上 x 的值在 b 和 r 的范围内并且最大值减去 x 的值在 l 和 a 的范围内,则可以先将最小值和最大值加上减去 x,此时两点的距离必大于 x,需要移动三步
{
cout << 3 << '\n';
return;
}
cout << -1 << '\n'; // 上述几种情况都不满足则代表无法从 a 移动到 b
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// scanf("%d", &T);
cin >> T;
while (T--)
{
solve();
}
return 0;
}
标签:思维,cout,10,int,最小值,temperature,test,Thermostat
From: https://www.cnblogs.com/KSzsh/p/16905902.html