• R - Sonya and Hotels


    Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.

    The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has nn hotels, where the ii-th hotel is located in the city with coordinate xixi. Sonya is a smart girl, so she does not open two or more hotels in the same city.

    Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to dd. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel.

    Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original nn hotels to the new one is equal to d.

    Input

    The first line contains two integers nn and dd (1≤n≤1001≤n≤100, 1≤d≤1091≤d≤109) — the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.

    The second line contains nn different integers in strictly increasing order x1,x2,…,xn (−109≤xi≤109) — coordinates of Sonya's hotels.

    Output

    Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to d.

    Sample 1

    InputcopyOutputcopy
    4 3
    -3 2 9 16
    
    6
    

    Sample 2

    InputcopyOutputcopy
    5 2
    4 8 11 18 19
    
    5
    

    Note

    In the first example, there are 66 possible cities where Sonya can build a hotel. These cities have coordinates −6, 5, 6, 12, 13, and 19.

    In the second example, there are 55 possible cities where Sonya can build a hotel. These cities have coordinates 2, 6, 13, 16, and 21.

    题意翻译

    一根数轴上已经有了 n 家酒店, 且坐标已知. 现在想要新建一家酒店, 要求这家酒店与其他所有酒店的距离的最小值等于 d , 问有多少种方案?

    输入:

    第一行有两个整数 n 和 d (1 <= n <= 100, 1 <= d <= 1e9), 分别表示酒店的数目和新酒店到其他所有酒店的距离的最小值.

    第二行有严格递增的 n 个整数 x1, x2, ..., xn (-1e9 <= xi <= 1e9), 分别表示现有酒店的坐标.

    输出:

    方案数量

    1. #include<iostream>
    2. #include<algorithm>
    3. using namespace std;
    4. const int N = 110;
    5. int n, d;
    6. int a[N], b[N];
    7. int main()
    8. {
    9. cin >> n >> d;
    10. int sum = 0;
    11. for (int i = 1; i <= n; i++) cin >> a[i];
    12. for (int i = 1; i <= n-1; i++) b[i] = a[i+1] - a[i]; //计算相邻元素之间的差
    13. for (int i = 1; i <= n; i++)
    14. {
    15. if (b[i] > 2 * d) sum += 2; //如果相邻元素之间的差大于2*d,则可以建两家酒店
    16. if (b[i] == 2 * d) sum++; //等于则可以建一家酒店,小于就无法建酒店
    17. }
    18. cout << sum+2 << endl; //最后输出的时候考虑两个端点,左端点的左侧,右端点的右侧各可以建一家酒店
    19. }

  • 相关阅读:
    《为你护航-网络空间安全科普读本》读书笔记
    Chief Ray and Margin Ray and Principle Ray(主光线和边缘光线)
    RestTemplate (二) : RestOperations、具体API使用、RestTemplate原理介绍、使用案例
    STM32第二十课:FreeRTOS任务管理和信号量
    Web大学生网页作业成品:基于html制作中国科技发展网站设计题材【航天之路7页】HTML+CSS+JavaScript
    除法类型复合指标异动贡献度计算
    Win10操作系统安装Python
    kafka与zookeeper的SSL认证教程
    Python3.9标准库math中的函数汇总介绍(53个函数和5个常数)
    Springboot+高校教材预订信息管理系统 毕业设计-附源码150905
  • 原文地址:https://blog.csdn.net/GF0919/article/details/132824523