Base Station Sites(跳石头问题)

这是一个关于如何选择基站位置以确保至少覆盖指定数量站点的编程问题。给定候选基站位置列表,需要找到最短的基站间隔,使得至少有指定数量的站点被覆盖。通过二分查找算法来确定最佳基站间距。

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

 Base Station Sites

时间限制: 1 Sec   内存限制: 128 MB
提交: 214   解决: 86
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

5G is the proposed next telecommunications standards beyond the current 4G standards. 5G planning aims at higher capacity than current 4G, allowing a higher density of mobile broadband users, and supporting device-to- device, reliable, and massive wireless communications. A telecommunication company would like to install more base stations to provide better communication for customers. Due to the installation cost and available locations, the company can only install S (2 ≤ S ≤ L) base stations at L (2 ≤ L ≤ 100, 000) candidate locations.  Since the  base stations work in the same frequency band, they will interfere and cause severe performance degradation. To provide high quality communication experience to customers, the company would like to maximize the distance between the base stations so as to reduce the wireless interference among the base stations. Suppose the L candidate  locations are in a straight line at locations P1, P2,..., PL (0 ≤ Pi ≤ 1, 000, 000) and the company wants to install  S base stations at the L candidate locations. What is the largest minimum distance among the S base stations?

输入

The input data includes multiple test sets.
Each set starts with a line which specifies L (i.e., the number of candidate locations) and S (i.e., the number of base stations). The next line contains L space-separated integers which represent P1, P2,..., PL.
The input data ends “0 0”.

输出

For each set, you need to output a single line which should be the largest minimum distance among the base stations.

样例输入

5 3  
2 3 9 6 11
4 3  
1 4 9 10
0 0  

样例输出

4
3


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int S,L,a[N];
int main()
{
    while(scanf("%d%d",&L,&S)&&L+S)///输入候选位置的数量,基站的数量
    {///并且l和s为0时结束输入
        for(int i=0; i<L; i++) scanf("%d",&a[i]); ///可能选中的位置
        sort(a,a+L);///从小到大排序,便于寻找位置
        int l = 0, r = 1e6,mid;///左面,右面,中间
        while(l<=r)///当左面小于右面时进行循环
        {
            mid = (l+r)>>1;///中间等于左右和的一半
            int cnt = 1, now = 0;///now记录当前位置,cnt表示可以放置基站的数量
            for(int i=1; i<L; i++)///遍历候选位置
            {
                if(a[i]-a[now]>=mid)///如果两者之间距离大于mid的话
                {
                    now = i;///更新当前位置
                    cnt++;///可建立基站数+1
                }
            }
            if(cnt>=S) l = mid + 1;///如果可建立基站数大于s,说明距离小了,调大距离
            else r = mid - 1;///否则,调小距离
        }
        printf("%d\n",r);///或者l-1;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值