• 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):

    1. #include
    2. using namespace std;
    3. #define endl '\n'
    4. typedef long long ll;
    5. typedef unsigned long long ull;
    6. const int maxn = 1e3 + 10;
    7. const int INF = 0x3fffffff;
    8. /*
    9. 显然只有五种可能的答案:0、-1、1、2、3。
    10. 即要么无需操作,要么无解,要么最多只需要三步。
    11. 当a == b时,答案为0.
    12. 如果a和b的差值大于等于x,那么我们一步即可。
    13. 否则我们可能采取的操作有:
    14. 两步:先向高位或者低位方向移动,然后反向移动到b。
    15. 三步:先向高位/低位方向移动,然后再向低位/高位方向移动,再移动到b。
    16. 操作的前提是移动的时候不超过l和r。所以判断a、b和l、r之间的差值即可。
    17. */
    18. void solve()
    19. {
    20. int l, r, x, a, b;
    21. cin >> l >> r >> x >> a >> b;
    22. if (a == b)
    23. {
    24. cout << 0 << endl;
    25. return;
    26. }
    27. if (abs(a - b) >= x)
    28. {
    29. cout << 1 << endl;
    30. return;
    31. }
    32. if (b - l >= x && r - b >= x) // b可以从l到达,也可以从r到达
    33. {
    34. if (a - l >= x || r - a >= x)
    35. cout << 2 << endl; // a可以到达l,或者可以到达r,那么我们就可以从a到l或者r,然后移动到b,两步
    36. else
    37. cout << -1 << endl; // a无法到达l或者r,无解
    38. }
    39. else if (b - l >= x) // b可以从l到达
    40. {
    41. if(a - l >= x)
    42. cout << 2 << endl; // a可以到达l,那么我们可以从a到达l,然后再移动到b,两步
    43. else if(r - a >= x)
    44. cout << 3 << endl; // a不可以直接到达l,但是可以到达r,那么我们可以先到达r,再从r往l,然后到达b
    45. else
    46. cout << -1 << endl; // a无法到达l或者r,无解
    47. }
    48. else if (r - b >= x) // b可以从r到达
    49. {
    50. if(r - a >= x)
    51. cout << 2 << endl; // a可以到达r,那么我们可以从a到达r,然后再移动到b,两部
    52. else if(a - l >= x)
    53. cout << 3 << endl; // a不可以直接到达r,但是可以到达l,那么我们可以先到达l,再从l往r,然后到达b
    54. else
    55. cout << -1 << endl; // a无法到达l或者r,无解
    56. }
    57. else
    58. cout << -1 << endl;
    59. }
    60. int main()
    61. {
    62. ios::sync_with_stdio(false);
    63. cin.tie(0);
    64. cout.tie(0);
    65. cout << fixed;
    66. cout.precision(18);
    67. int t;
    68. cin >> t;
    69. while (t--)
    70. {
    71. solve();
    72. }
    73. return 0;
    74. }

  • 相关阅读:
    WebGL 与 WebGPU比对[4] - Uniform
    Java Streams:流操作及示例
    ArcGIS Engine基础(29)之加载arcgis server切片地图服务
    什么是哈希表
    JVM参数查看与设置
    记录一次mysql死锁
    猪齿鱼平台常用前端css实现方案
    在阿里云的函数计算上部署
    Redis变慢?深入浅出Redis性能诊断系列文章(一)
    oracle rman restore database的时候报错RMAN-06023: 没有找到数据文件1的副本来还原
  • 原文地址:https://blog.csdn.net/qq_45554473/article/details/127939839