题目链接
https://vjudge.net/problem/CodeForces-1004A
题意
给n个旅馆的位置,要新建一个旅馆,使得它到其他旅馆的最小距离为k,问可以建多少个这样的旅馆。
思路
给n个旅馆排序,对相邻的两个旅馆
- 如果距离大于2k,则中间可以建两个。
- 如果距离等于2k,则中间可以建一个。
- 如果距离小于2k,则不能建。
AC代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<sstream>
#include<cctype>
#include<map>
#include<stack>
#include<queue>
#include<list>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
const double PI=atan(1.0)*4;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1000000007;
const int maxn = 210;
int a[maxn];
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n, d, ans = 2;
scanf("%d%d", &n, &d);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for(int i = 1; i < n; i++)
{
int x = a[i] - a[i - 1];
if(x > 2 * d)
ans += 2;
else if(x == 2 * d)
ans++;
}
printf("%d\n", ans);
return 0;
}