二分

5G是超出当前4G标准的提议的下一个电信标准。 5G规划的目标是提供比当前4G更高的容量,从而允许更高密度的移动宽带用户,并支持设备到设备,可靠的大规模无线通信。一家电信公司希望安装更多的基站,以便为客户提供更好的通信。由于安装成本和可用位置的限制,该公司只能在L(2≤L≤100,000)个候选位置安装S(2≤S≤L)个基站。由于基站工作在相同的频带中,因此它们会干扰并导致严重的性能下降。为了向客户提供高质量的通信体验,该公司希望最大程度地扩大基站之间的距离,以减少基站之间的无线干扰。假设L个候选位置在直线上位于位置P1,P2,...,PL(0≤Pi≤1,000,000),并且公司希望在L个候选位置安装S个基站。 S个基站之间最大的最小距离是多少?
输入
输入数据包括多个测试集。
每一组以指定L(即,候选位置的数量)和S(即,基站的数量)的行开始。下一行包含L个以空格分隔的整数,分别表示P1,P2,...,PL。
输入数据以“ 0 0”结束。
输出
对于每个集合,您需要输出一条线路,该线路应该是基站之间最大的最小距离。
样例输入
5 3
2 3 9 6 11
4 3
1 4 9 10
0 0
样例输出复制
4
3
提示
对于第一组,可以将3个基站安装在位置2、6、11。

最小距离最大化:二分

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue> 
#include<algorithm>
using namespace std;
typedef long long ll;
template <typename Tp>
void read(Tp &x){//read(n);
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch>'9'||ch<'0')){
        ch=getchar();
    }
    if(ch=='-'){
        fh=-1;ch=getchar();
    }else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
    }
    x*=fh;
}
inline char read1()//字符串读入挂
{
    register char ch=getchar();
    while(ch<'A'||ch>'M')ch=getchar();
    return ch; 
}
const int maxn=1e6+100;
const int mod=1000000007;
int a[maxn];
int n,m;
int judge(int x){
    int p=1;
    int z=1;
    for(int i=2;i<=n;i++){
        if(a[i]-a[z]>=x){
            p++;
            z=i;
        }
    }
    if(p>=m){
        return 1;
    }
    else{
        return 0;
    }
}
int main(){
    while(cin>>n>>m){
        if(n==0&&m==0){
            break;
        }
        int sum=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+n+1);
        int l=1,r=1e9;
        int ans;
        while(l<=r){
            int mid=(l+r)/2;
            if(judge(mid)){
                l=mid+1;
                ans=mid;
            } 
            else{
                r=mid-1;    
            }
        } 
        cout<<ans<<endl;
    }
}

 

posted @ 2020-10-04 20:23  哎呦哎(iui)  阅读(202)  评论(0编辑  收藏  举报