Sonya and Hotels CodeForces - 1004A (水题)

A. Sonya and Hotels
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 dd.

Input

The first line contains two integers nn and dd (1n1001≤n≤1001d1091≤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,,xnx1,x2,…,xn (109xi109−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 dd.

Examples
input
Copy
4 3
-3 2 9 16
output
Copy
6
input
Copy
5 2
4 8 11 18 19
output
Copy
5
Note

In the first example, there are 66 possible cities where Sonya can build a hotel. These cities have coordinates 6−6556612121313, and 1919.

In the second example, there are 55 possible cities where Sonya can build a hotel. These cities have coordinates 226613131616, and 2121



扫描二维码关注公众号,回复: 2243379 查看本文章

题意:给出一个长为n的数列,表示在一位坐标中,已近建好的酒店。给出一个d,表示,如果要新建一些酒店,那么相邻的两个酒店之间的最小距离不得小于d(酒店只能处于整数位置)。问,最多可以建多少个新的酒店。(保证按升序给出数列)


思路:每一个酒店的左边是否可以建酒店,由上一个酒店的位置决定,而一个酒店的右边是否可以建酒店,由给出数列的下一个酒店的位置决定。那么,我们只需要从第2个酒店开始判断这个酒店的左边是否可以建新的酒店即可。最后答案要加上2,因为,第一个酒店的左边和最后一个酒店的右边一定可以建酒店。(注意,我们有l记录上一个酒店的位置,r记录当前酒店的位置,如果l+d<r-d,说明在这之间可以建立两个酒店,如果是等于,那么只可以建一个酒店,否者不能建酒店)


#include "iostream"
using namespace std;
int main()
{
    int ans=0;
    int n,d,l,r;
    cin>>n>>d;
    for(int i=1;i<=n;i++){
        cin>>r;
        if(i!=1){
            if(r-d>l+d) ans+=2,l=r;
            else if(r-d==l+d) ans++,l=r;
        }
        l=r;
    }
    cout<<ans+2<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80960461
今日推荐