NOIP2015提高组 跳石头 ACM-ICPC2017香港 E(选择/移除+二分答案)

跳石头

题目背景

一年一度的“跳石头”比赛又要开始了!

题目描述

这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有 NN 块岩石(不含起点和终点的岩石)。在比赛过程中,选手们将从起点出发,每一步跳向相邻的岩石,直至到达终点。

为了提高比赛难度,组委会计划移走一些岩石,使得选手们在比赛过程中的最短跳跃距离尽可能长。由于预算限制,组委会至多从起点和终点之间移走 MM 块岩石(不能移走起点和终点的岩石)。

输入输出格式

输入格式:

 

第一行包含三个整数 L,N,ML,N,M,分别表示起点到终点的距离,起点和终点之间的岩石数,以及组委会至多移走的岩石数。保证 L \geq 1L1 且 N \geq M \geq 0NM0。

接下来 NN 行,每行一个整数,第 ii 行的整数 D_i( 0 < D_i < L)Di(0<Di<L), 表示第 ii 块岩石与起点的距离。这些岩石按与起点距离从小到大的顺序给出,且不会有两个岩石出现在同一个位置。

 

输出格式:

 

一个整数,即最短跳跃距离的最大值。

 

输入输出样例

输入样例#1: 复制
25 5 2 
2
11
14
17 
21
输出样例#1: 复制
4

说明

输入输出样例 1 说明:将与起点距离为 22和 1414 的两个岩石移走后,最短的跳跃距离为 44(从与起点距离 1717 的岩石跳到距离 2121 的岩石,或者从距离 2121 的岩石跳到终点)。

另:对于 20\%20%的数据,0 ≤ M ≤ N ≤ 100MN10。

对于50\%50%的数据,0 ≤ M ≤ N ≤ 1000MN100。

对于 100\%100%的数据,0 ≤ M ≤ N ≤ 50,000,1 ≤ L ≤ 1,000,000,0000MN50,000,1L1,000,000,000。

 

 

#include<bits/stdc++.h>
#define MAX 50005
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,i,j;
    int L,N,M;
    scanf("%d%d%d",&L,&N,&M);
    a[0]=0;
    for(i=1;i<=N;i++){
        scanf("%d",&a[i]);
    }
    a[N+1]=L;
    int l=0,r=1000000000,m;
    int ans=0;
    while(l<=r){
        m=(l+r)/2;
        int pre=0,cnt=1;
        for(i=1;i<=N+1;i++){
            if(a[i]-a[pre]>=m){
                pre=i;
                cnt++;
            }
            
        }
        if(cnt>=N+2-M){
            ans=max(ans,m);
            l=m+1;
        }
        else{
            r=m-1;
        }
    }
    printf("%d\n",ans);
    return 0;
}
跳石头(移除)

 

 

E

  •  61.05%
  •  2000ms
  •  262144K
 

5G5G is the proposed next telecommunications standards beyond the current 4G4G standards. 5G5G planning aims at higher capacity than current 4G4G, allowing a higher density of mobile broadband users, and supporting device-to- device, reliable, and massive wireless communications. AA 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 \le S \le L)S(2SL) base stations at L (2 \le L \le 100, 000)L(2L100,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 P_1, P_2, ... , PL (0 \le Pi \le 1, 000, 000)P1,P2,...,PL(0Pi1,000,000) and the company wants to install SS base stations at the LL candidate locations. What is the largest minimum distance among the SS base stations?

Input

The input data includes multiple test sets.

Each set starts with a line which specifies LL (i.e.i.e., the number of candidate locations) and SS (i.e.i.e., the number of base stations). The next line contains LL space-separated integers which represent P_1P1 , P_2P2 , ... , P_LPL . The input data ends “000”.

Output

For each set, you need to output a single line which should be the largest minimum distance among the base stations.

For the first set, the 33 base stations can be installed at locations 2, 6, 112,6,11.

样例输入

5 3
2 3 9 6 11
4 3
1 4 9 10
0 0

样例输出

4
3

题目来源

ACM-ICPC 2017 Asia HongKong

 

#include<bits/stdc++.h>
#define MAX 100005
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,i,j;
    int n,s;
    while(scanf("%d%d",&n,&s)&&n+s){
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        int l=0,r=1000000,m;
        int ans=0;
        while(l<=r){
            m=(l+r)/2;
            int pre=1,cnt=1;
            for(i=2;i<=n;i++){
                if(a[i]-a[pre]>=m){
                    pre=i;
                    cnt++;
                }
                
            }
            if(cnt>=s){
                ans=max(ans,m);
                l=m+1;
            }
            else{
                r=m-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
E(选择)

 

posted @ 2018-09-30 14:38  yzm10  阅读(465)  评论(0编辑  收藏  举报