【NOIP2015提高组】跳石头

https://www.luogu.org/problem/show?pid=2678

最小值最大问题,二分答案。每次检查是否能仅移走m块岩石使得所有跳跃距离均大于等于mid。

#include <iostream>
#include <list>
#define maxn 50005
const int inf = 0x7fffffff;
using namespace std;
int l, n, m;
int dist[maxn];
bool check(int k);
int main()
{
    cin >> l >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> dist[i];
    dist[++n] = l;

    int left = 0, right = 1000000005, mid;
    while (left < right)
    {
        mid = (left + right + 1) / 2;
        if (check(mid)) // 能否仅通过移去m块石头使所有跳跃距离≥mid
            left = mid;
        else
            right = mid - 1;
    }
    cout << left << endl;
    return 0;
}
bool check(int k)
{
    int cnt = 0;
    int last = 0;
    for (int i = 1; i <= n; i++)
    {
        if (dist[i] - dist[last] < k)
            cnt++;
        else
            last = i;
        if (cnt > m)
            return false;
    }
    return true;
}

 

posted @ 2017-09-16 16:25  ssttkkl  阅读(485)  评论(0编辑  收藏  举报