Base Station Sites二分查找答案ICPC 2017 HongKong

寒。

问题 E: Base Station Sites

时间限制: 1 Sec   内存限制: 128 MB
提交: 166   解决: 63
[ 提交][ 状态][ 讨论版][命题人: 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

提示

For the first set, the 3 base stations can be installed at locations 2, 6, 11.

想了很久。。。明知道是个二分不知道怎么写。。。数据有序,两头已经知道,数据范围也合适,很明显的二分。。。但是苦于技术有限。。。想了很久也不知道。。。如何实现二分,这里补题。。。以防后患

题目:

有L个数,从中选出S个数,使得任意两个数差的绝对值 的 最小值 最大

首先题目说的是 largest minimum distance 我们要使距离尽可能的大,那么两头肯定是要取的,于是问题就转化为了在L中选S个数,使得他们数差的绝对值的最小值尽可能的大,意思就是数据越离散越好。

首先输入并排序

然后从1 到 e6 (题目给的范围)每次二分得到一个解,这个解假设是一个答案,我们判断是否满足所有距离大于等于它的点个数是大于等于要求的S,换句话说。。。就是判断这个mid是否为一个可行解,判断条件就是否题目中以mid作为数差的绝对值的最小值,然后暴力1->L,判断是否有大于S个SITE可以放入:

 for (int i=2;i<=L;i++)
            {
                if(a[i]-a[now]>=mid) //满足答案mid条件
                {
                    cnt++;//个数
                    now=i;更新区间
                }
            }
            if(cnt>=S)//mid值是满足题意的那么我们可以寻找更优的小的
            {
                l=mid+1;//l=mid+1相当于左边的
            }
            else
            {
                r=mid-1;
            }

然后我们得到一个mid()他是目前的解,意思就是在他是minimum distance 的解 但是,它并不是答案,答案要求是的是largest我们需要通过让l=mid+1;使得数差尽可能大,而我们保证了,mid一定是minimum distance所以最后二分两边相同即可得到答案

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int main()
{
    int L,S;
    while (~scanf("%d%d",&L,&S) && L+S)
    {
        for (int i=1; i<=L; i++)
        {
            scanf("%d",&a[i]);
        }
        int l=1;//左边
        int r=1e6;//右边
        sort(a+1,a+L+1);
        while (l<=r)  //当左边等于右边时
        {
            int cnt=1;
            int now=1;
            int mid=(l+r)/2;
            printf("%d %d %d\n",l,r,mid);
            for (int i=2;i<=L;i++)
            {
                if(a[i]-a[now]>=mid) //寻找满足答案mid条件的个数
                {
                    cnt++;
                    now=i;
                }
            }
            if(cnt >= S)//mid值是满足题意的那么我们可以寻找更优的小的
            {
                l=mid+1;
            }
            else
            {
                r=mid-1;
            }
        }
        printf("%d\n",l-1);
    }
    return 0;
}

l-1的原因是我们当l==r进入时,由于满足答案l会被mid+1而mid就是解因此减回来就好了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值