CodeForces 1004A

本文介绍了一个算法问题:如何计算在给定多个旅馆位置的情况下,能够新建多少个旅馆,使它们与现有旅馆之间的最小距离达到指定值。通过排序和比较相邻旅馆间的距离来确定可能的建设位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值