NOIP2015提高组day2 —— 跳石头(stone)

#include <fstream>
using namespace std;
ifstream fin("stone.in");
ofstream fout("stone.out");
int L, N, M, a[100000];
bool OK(int len)
{
    int lpoint=0, move_num=0;
    for (int i=0; i<=N; i++)
        if (a[i] - lpoint < len)
            move_num++;
        else
            lpoint=a[i];
    return move_num <= M;
}
int main()
{
    fin>>L>>N>>M;
    for (int i=0; i<N; i++)
        fin>>a[i];
    a[N]=L;
    int lowerlim=0, upperlim=L+1, middle;
    while (lowerlim+1 < upperlim)
    {
        middle = (lowerlim+upperlim)>>1;
        if (OK(middle))
            lowerlim=middle;
        else
            upperlim=middle;
    }
    fout<<lowerlim<<endl;
    return 0;
}

如果作死用穷举,只能过小数据,因为时间复杂度是 O(2M) ,所以必须用二分查找,时间复杂度为 O(NlogL)

你可能感兴趣的:(NOIP2015)