【POJ】3258

来源:互联网 发布:软件汉化工具 编辑:程序博客网 时间:2024/06/06 10:05

http://poj.org/problem?id=3258

从N块石头中移除M块,使得间距最小值最大。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn=50005;int n,m,L;int dis[maxn];bool check(int mid){  //最小值     int now=0;    int cnt=0;    int i=1;    while (i<=n+1){        if (dis[i]-dis[now]>=mid){            i++;            now=i-1;        }        else {            i++;            cnt++;        }    }    if (cnt<=m) return true;  //<=m说明最小值还可以更大     else return false;       }int main(){    cin >> L >> n >> m;    for (int i=1;i<=n;i++){        cin >> dis[i];    }    dis[0]=0;    dis[n+1]=L;    sort(dis,dis+n+1);    int l=0,r=1000000005;    int mid;    while (l+1<r){        mid=(l+r)/2;        if (check(mid)) l=mid;        else r=mid;    }    cout << l << endl;}
原创粉丝点击