洛谷 P2678 跳石头【经典二分答案/贪心】

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

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

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

接下来 NN 行,每行一个整数,第 ii 行的整数 D_i( 0 < D_i < L)D
i
​ (0<D
i
​ <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 ≤ 100≤M≤N≤10 。

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

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

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int L,n,m;
int a[maxn];
/*
25 5 2
2
11
14
17
21

4
发现如果两个点之间距离小于最小跳跃距离, 那么就必须将其中一个石子移去.假设这两个点之前所有石子已经满足要求了, 发现如果移去前一个石子, 可是前一个石子与它前面的石子本来就大于最短距离, 移去后后面的石子与再后面的石子可能还是大于最短距离;  
如果移走后面的石子, 不会改变前面石子, 那么它前面石子与后面的石子的距离减小了, 也就是.
*/
int check(int x)
{
    int now=0,cnt=0;
    rep(i,0,n+1)
    {
        if(a[i]-now<x) cnt++;//如果距离小于我二分的答案x,那么这块石头需要搬走
        else now = a[i]; //不然我就跳过来
    }
    return cnt<=m;
}
void solve()
{
    int l=0,r=L,mid,ans;
    while(l<=r)
    {
        mid = (l+r)/2;
        if(check(mid))
        {l=mid+1;ans=mid;}
        else r=mid-1;
    }
    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d%d",&L,&n,&m))
    {
        rep(i,0,n)
        {
            scanf("%d",&a[i]);
        }
        a[n]=L;
        solve();
    }
}

猜你喜欢

转载自www.cnblogs.com/Roni-i/p/9362907.html