KSzsh

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Thermostat(思维)

题目链接

题目描述:

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 |ab|x and lbr.

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 (1t104) — 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 (109lr109, 1x109) — range of temperature and minimum temperature change.

The second line of each case contains two integers a and b (la,br) — 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.

Note:

In the first example, the thermostat is already set up correctly.

In the second example, you can achieve the desired temperature as follows: 4105.

In the third example, you can achieve the desired temperature as follows: 3827.

In the fourth test, it is impossible to make any operation.

AC代码:

#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 和 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;
}

posted on   KSzh  阅读(180)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示