码迷,mamicode.com
首页 > 其他好文 > 详细

路由器安置(Routing)

时间:2015-02-25 15:35:56      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:二分

题目

【问题描述】

  一条街道安装无线网络,需要放置M个路由器。整条街道上一共有N户居民,分布在一条直线上,每一户居民必须被至少一台路由器覆盖到。现在的问题是所有路由器的覆盖半径是一样的,我们希望用覆盖半径尽可能小的路由器来完成任务,因为这样可以节省成本。

【输入数据】

  输入文件第一行包含两个整数M和N,以下N行每行一个整数Hi表示该户居民在街道上相对于某个点的坐标。

【输出数据】

  输出文件仅包含一个数,表示最小的覆盖半径,保留一位小数。

【样例输入】

  2 3
  1
  3
  10

【样例输出】

  1.0

【数据规模】

对于60%的数据,有1 ≤ N, M ≤ 100,-1000 ≤ Hi ≤ 1000;
对于100%的数据,有1 ≤ N, M ≤ 100000,-10000000 ≤ Hi ≤ 10000000。

代码

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>
#define R0(i,n) for(int i=0;i<n;++i)
#define R1(i,n) for(int i=1;i<=n;++i)
#define cl(x,c) memset(x,c,sizeof x)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
template <class T> inline void read(T&x){
    bool f = false; char ch;
    for (ch = getchar(); ch <= 32; ch = getchar());
    if (ch == ‘-‘) f = true, ch = getchar();
    for (x = 0; ch > 32; ch = getchar()) x = x * 10 + ch - ‘0‘;
    if (f) x = -x;
}
template <class T> inline void write(T x){
    if (x < 0) putchar(‘-‘), x = -x;
    if (x < 10)
        putchar(x + ‘0‘);
    else
        write(x / 10), putchar(x % 10 + ‘0‘);
}
void setIO(string t){
    string a=t+".in",b=t+".out";
    freopen(a.c_str(),"r",stdin); 
    freopen(b.c_str(),"w",stdout);
}
int S[100005];
int n,m,t;
bool check(int d){
    int cnt=0,lastp=0;
    for(;lastp<n;lastp=upper_bound(S,S+n,S[lastp]+d)-S,cnt++);
    if(cnt<=m)return 1;else return 0;
}
int main(){
    setIO("routing");
    read(m),read(n);
    if(m>=n){puts("0.0");return 0;}
    R0(i,n)read(S[i]);
    sort(S,S+n);
//  unique(S,S+n);
    int r=S[n-1]-S[0],l=0;
    while(l<r){
        int mi=(l+r)>>1;
        if(check(mi))r=mi;
        else l=mi+1;
    }
    printf("%.1lf",(double)l/2.0);
}

感谢wxjlzbcd的脑洞,wfwbz的指导。
二分太神了orz

路由器安置(Routing)

标签:二分

原文地址:http://blog.csdn.net/iostream0/article/details/43937779

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!