Codeforces-1004B. Sonya and Exhibition

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 xi. 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 (1n1001d109) — 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 (109xi109) — 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
4 3
-3 2 9 16
Output
6
Input
5 2
4 8 11 18 19
Output
5

Note

In the first example, there are 6 possible cities where Sonya can build a hotel. These cities have coordinates 6561213, and 19.

In the second example, there are 5 possible cities where Sonya can build a hotel. These cities have coordinates 261316 and 21.

题意:

给一个数轴,求到输入的各个点距离为d的点的个数之和。

注意:不要忘记数轴之外的两个点,在结果处加上。

我的代码:

#include<iostream>
using namespace std;
typedef long long ll;
ll a[120];
int main()
{
  ll m,d,ans=0;
  cin>>m>>d;
  for(int i=1;i<=m;++i)
  {
    cin>>a[i];
  }
  for(int i=1;i<=m-1;i++)
  {
    int temp=a[i]+d;
    if(a[i+1]-temp>d) ans+=2;
    else if(a[i+1]-a[i]==2*d) ans+=1;

  }
  cout<<ans+2<<endl;

}

神级队友徐晨松的代码:

Codeforces#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    long long n,d;
    int a[105];
    double b[105];
    int k=0;
    int sum=0;
    cin>>n>>d;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        b[k++]=((a[i]-a[i-1])*1.0/d);
    }
    for(int i=0;i<k;i++)
    {
        //cout<<b[i]<<" "<<endl;
        if(b[i]>2)sum+=2;
        else if(b[i]==2)sum+=1;
    }
    cout<<sum+2<<endl;
    return 0;
}

 

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注

1 × 2 =