• CF 1895A 学习笔记 分类讨论


    A. Treasure Chest

    time limit per test

    2 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    Monocarp has found a treasure map. The map represents the treasure location as an OX axis. Monocarp is at 00, the treasure chest is at 𝑥�, the key to the chest is at 𝑦�.

    Obviously, Monocarp wants to open the chest. He can perform the following actions:

    • go 11 to the left or 11 to the right (spending 11 second);
    • pick the key or the chest up if he is in the same point as that object (spending 00 seconds);
    • put the chest down in his current point (spending 00 seconds);
    • open the chest if he's in the same point as the chest and has picked the key up (spending 00 seconds).

    Monocarp can carry the chest, but the chest is pretty heavy. He knows that he can carry it for at most 𝑘� seconds in total (putting it down and picking it back up doesn't reset his stamina).

    What's the smallest time required for Monocarp to open the chest?

    Input

    The first line contains a single integer 𝑡� (1≤𝑡≤1001≤�≤100) — the number of testcases.

    The only line of each testcase contains three integers 𝑥,𝑦�,� and 𝑘� (1≤𝑥,𝑦≤1001≤�,�≤100; 𝑥≠𝑦�≠�; 0≤𝑘≤1000≤�≤100) — the initial point of the chest, the point where the key is located, and the maximum time Monocarp can carry the chest for.

    Output

    For each testcase, print a single integer — the smallest time required for Monocarp to open the chest.

    Example

    input

    Copy

     
    

    3

    5 7 2

    10 5 0

    5 8 2

    output

    Copy

    7
    10
    9
    

    Note

    In the first testcase, Monocarp can open the chest in 77 seconds with the following sequence of moves:

    • go 55 times to the right (55 seconds);
    • pick up the chest (00 seconds);
    • go 22 times to the right (22 seconds);
    • pick up the key (00 seconds);
    • put the chest down (00 seconds);
    • open the chest (00 seconds).

    He only carries the chest for 22 seconds, which he has the stamina for.

    In the second testcase, Monocarp can pick up the key on his way to the chest.

    In the third testcase, Monocarp can't use the strategy from the first testcase because he would have to carry the chest for 33 seconds, while he only has the stamina for 22 seconds. Thus, he carries the chest to 77, puts it down, moves 11 to the right to pick up the key and returns 11 left to open the chest.

    链接

    传送门

    代码

    1. //axis 轴
    2. //chest 箱子
    3. //stamina 耐力
    4. //k 表示这个人能坚持多少秒拿着这个箱子
    5. //x 表示箱子的地点
    6. //y 表示钥匙的地点
    7. #include
    8. using namespace std;
    9. int main()
    10. {
    11. int t;
    12. scanf("%d",&t);
    13. while(t--)
    14. {
    15. int x,y,k;
    16. scanf("%d%d%d",&x,&y,&k);
    17. if(x
    18. {
    19. if(x+k==y||x+k>y) printf("%d\n",y);
    20. else
    21. {
    22. int ans=x+k;
    23. int temp=y-ans;
    24. temp*=2;
    25. ans+=temp;
    26. printf("%d\n",ans);
    27. }
    28. }
    29. else
    30. {
    31. printf("%d\n",x);
    32. }
    33. }
    34. return 0;
    35. }

    总结

    1.分类讨论,分成两种情况,第一种情况,箱子在钥匙左边,第二种情况,钥匙在箱子左边,如果是第二种情况,直接输出箱子的坐标即可

    2.如果是第一种情况,主角可以背着箱子往右边走,但是最多走k个单位(坚持k秒,每秒一个单位) ,如果x+k可以刚好到达钥匙所在的位置,或者超过钥匙所在的位置,直接输出钥匙的坐标即可

    3.如果x+k不能到达钥匙所在的位置,就说明主角走到x+k位置的时候,要走去钥匙所在位置,然后再折返回来。[y-(x+k)]*2+(x+k)就是答案

    4.仔细慢慢读题,把题读懂非常重要

     

  • 相关阅读:
    面试必问系列:MySQL 索引合并优化及底层原理
    GEE:求最大值的几种方法
    Android studio安装详细教程
    状压dp和状态机dp题目汇总(持续更新)
    PAT.1139 First Contact
    小黑回了一波儿血,抽空想了想KMP的leetcode之旅:1367. 二叉树中的列表(在操场上看到了中老黑牵手)
    输入网址到网页显示,期间发生了什么?(收藏篇)
    一文读懂云渲染“串流”全链路时延及优化策略
    SpringCloudAliBaba篇(七)之Seata ---> 分布式事务组件
    C语言:数据的存储
  • 原文地址:https://blog.csdn.net/L3102250566/article/details/134540433