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

原题链接:

https://codeforces.com/contest/1759/problem/C

题目描述:

Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of aa.

The thermostat can only be set to a temperature from ll to rr inclusive, the temperature cannot change by less than xx. Formally, in one operation you can reconfigure the thermostat from temperature aa to temperature bb if |a−b|≥x|a−b|≥x and l≤b≤rl≤b≤r.

You are given ll, rr, xx, aa and bb. Find the minimum number of operations required to get temperature bb from temperature aa, or say that it is impossible.

Input

The first line of input data contains the single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each case contains three integers ll, rr and xx (−109≤l≤r≤109−109≤l≤r≤109, 1≤x≤1091≤x≤109) — range of temperature and minimum temperature change.

The second line of each case contains two integers aa and bb (l≤a,b≤rl≤a,b≤r) — the initial and final temperatures.

Output

Output tt numbers, each of which is the answer to the corresponding test case. If it is impossible to achieve the temperature bb, output -1, otherwise output the minimum number of operations.

题目大意:

给定一个温度范围 [l,r] 以及参数 m ,给定初始温度和目标温度,求最少次数将初始温度变为目标温度的操作数,每次切换温度必须保证温度变化大小大于等于 m 。

解题思路:

显然只有五种可能的答案:0、-1、1、2、3。

即要么无需操作,要么无解,要么最多只需要三步。

当a == b时,答案为0.

如果a和b的差值大于等于x,那么我们一步即可。

否则我们可能采取的操作有:

两步:先向高位或者低位方向移动,然后反向移动到b。

三步:先向高位/低位方向移动,然后再向低位/高位方向移动,再移动到b。

操作的前提是移动的时候不超过l和r。所以判断a、b和l、r之间的差值即可。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;

/*
    显然只有五种可能的答案:0、-1、1、2、3。
    即要么无需操作,要么无解,要么最多只需要三步。
    当a == b时,答案为0.
    如果a和b的差值大于等于x,那么我们一步即可。
    否则我们可能采取的操作有:
    两步:先向高位或者低位方向移动,然后反向移动到b。
    三步:先向高位/低位方向移动,然后再向低位/高位方向移动,再移动到b。
    操作的前提是移动的时候不超过l和r。所以判断a、b和l、r之间的差值即可。
*/

void solve()
{
    int l, r, x, a, b;
    cin >> l >> r >> x >> a >> b;
    if (a == b)
    {
        cout << 0 << endl;
        return;
    }
    if (abs(a - b) >= x)
    {
        cout << 1 << endl;
        return;
    }
    if (b - l >= x && r - b >= x)  // b可以从l到达,也可以从r到达
    {
        if (a - l >= x || r - a >= x) 
            cout << 2 << endl;  // a可以到达l,或者可以到达r,那么我们就可以从a到l或者r,然后移动到b,两步
        else
            cout << -1 << endl;  // a无法到达l或者r,无解
    }
    else if (b - l >= x)  // b可以从l到达
    {
        if(a - l >= x)
            cout << 2 << endl;  // a可以到达l,那么我们可以从a到达l,然后再移动到b,两步
        else if(r - a >= x)
            cout << 3 << endl;  // a不可以直接到达l,但是可以到达r,那么我们可以先到达r,再从r往l,然后到达b
        else
            cout << -1 << endl; // a无法到达l或者r,无解
    }
    else if (r - b >= x)  // b可以从r到达
    {
        if(r - a >= x)
            cout << 2 << endl;  // a可以到达r,那么我们可以从a到达r,然后再移动到b,两部
        else if(a - l >= x)
            cout << 3 << endl; // a不可以直接到达r,但是可以到达l,那么我们可以先到达l,再从l往r,然后到达b
        else
            cout << -1 << endl; // a无法到达l或者r,无解
    }
    else
        cout << -1 << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值