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

跳石头与二分法

时间:2018-04-05 21:04:27      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:which   location   输入   isp   first   std   play   VID   bool   

题目描述

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.

这题虽然不是跳石头,但可以用那种方法做 即让最小的两个点距离最大

技术分享图片
 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 int a[100005]={0};
 5 int n;
 6 bool fun(int x,int total)
 7 {
 8    int i=0;
 9    int cnt=-1;
10    int now=0;
11    while(i<n)
12    {
13        if(a[i]-a[now]<x)
14        {
15            cnt++;
16        }
17        else
18        {
19            now=i;
20        }
21        i++;
22    }
23    if(cnt<=total) return true;
24    return false;
25 }
26 int main()
27 {
28     int m,i;
29     while(~scanf("%d%d",&n,&m)&&n&&m)
30     {
31         int x=n-m;
32         int ans=-1;
33         memset(a,0,sizeof(a));
34         for(i=0;i<n;i++)
35         {
36             scanf("%d",&a[i]);
37         }
38         sort(a,a+n);
39         int mid;
40         int l=0,r=1000000;
41         while(l<r)
42         {
43              mid=(l+r)/2;
44             if(fun(mid,x))
45             {
46                 ans=mid;
47                 l=mid+1;
48             }
49             else
50             {
51                 r=mid;
52             }
53         }
54         printf("%d\n",ans);
55     }
56     return 0;
57 }
队友的代码

坑了队友一把 跟他说优化那个r和l

这道题我做过但不熟悉,要自己练 书上有二分法的代码

跳石头与二分法

标签:which   location   输入   isp   first   std   play   VID   bool   

原文地址:https://www.cnblogs.com/smallocean/p/8724043.html

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