首页 > 其他分享 >Thermostat(思维)

Thermostat(思维)

时间:2022-11-19 13:11:38浏览次数:70  
标签:思维 cout 10 int 最小值 temperature test 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 \(|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

相关文章

  • 20221005_T1C_思维dp
    题意一开始有\(n\)个颜色为黑白的球,但不知道黑白色分别有多少,\(m\)次操作,每次先拿出一个球,再放入黑白球各一个,再拿出一个球,最后拿出的球按顺序排列会形成一个颜色序列......
  • 全球名校AI课程库(33)| MIT麻省理工 · 计算思维导论(Julia)课程『Introduction to Comp
    ......
  • 【杂题】思维题做题笔记
    ###0.Update---2022.8.149:41发表此篇博文。2022.8.1414:48更改博文名为“【杂题】思维题做题笔记”。###1.AT2273[ARC066C]AdditionandSubtractionHard原......
  • Move Brackets(贪心+思维)
    题目链接题目描述:Youaregivenabracketsequence\(s\)oflength\(n\),where\(n\)iseven(divisiblebytwo).Thestring\(s\)consistsof\(\frac{n}{2}\)......
  • 思维分析逻辑 5 DAY
    目录如何分析结构分析对比分析时间序列相关性分析机器学习如何分析结构分析对比分析对比分析:所有的数据只有对比才有意义,每年的双11都会与之前的双11进行消费额对比;......
  • 141. 环形链表 ----- 哈希表、逆向思维、快慢指针
    给你一个链表的头节点head,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整......
  • 思维分析逻辑 3 DAY
    目录指标分析指标选择原则指标体系建立步骤流量分析渠道分析(从哪来)常见渠道及渠道分类渠道推广过程渠道的指标渠道分析方法转化及价值分析(经过什么?产生什么价值?)漏斗分析功......
  • 人工智能概论-思维导图版(原创)
              简要概述:人工智能AI研究始于上个世纪40年代,从人工智能概念诞生至今已经有60年,未来AI会构建有一定智慧能力的人工系统,以解放或延伸人类。在......
  • 思维分析逻辑 2 DAY
    目录数据分析多元思维模型微观能力中观能力宏观能力电商平台分析整体数据漏斗模型互联网金融分析授信模型了解芝麻信用分结构数据源的数据变量数据处理游戏数据分析常规指......
  • Day11.3:利用for循环打印三角形——思维详解
    利用for循环打印三角形要求:利用for循环打印出以下三角形思路与分析:观察三角形,每一行的左边其实都有打印内容的,只是被空格替换了;将左边空格的部分替换成*,补齐后会得......