ACM ICPC 2017 HongKong E-Base Station Sites (二分)

 E: Base Station Sites

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


【思路】

    最小中 找最大;

    二分 答案 判断 条件是否满足;

【代码实现】

/*
* Date:4/5/2018
* Tile: ICPC HongKong E 补
* Category: 二分
* AU:siz
* Attention: 二分答案  最小中找最大
* WA: 7
*/
#include <iostream>
#include <bits/stdc++.h>

typedef long long ll;
const int MAXN=1e5+5;
const int INF=0x3f3f3f3f;

using namespace std;
int n,m;
ll a[MAXN];
int judge(ll mid)
{
    int cot=0;
    ll sum=0;
    for(int i=2;i<=n;i++)
    {
        sum+= a[i]-a[i-1];
        if(sum>=mid)
        {
            cot++;
            sum=0;
        }
    }
    if(cot>=m)
        return 1;
    else
        return 0;
}
int main()
{

    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        m--;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+n+1);
        ll l=0,r=a[n]-a[1];
        ll ans=0;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(judge(mid))
                l=mid+1;
            else
                r=mid-1;
        }
        cout<<r<<endl;

    }
    return 0;
}



1233

posted @ 2018-04-05 21:21  Sizaif  阅读(352)  评论(0编辑  收藏  举报