洛谷P2678 跳石头

简简单单二分答案,n和m不要写反
Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000000 + 3;
int l,m,n,dis[maxn];
inline bool check(int tps)
{
    int pre = 0, cnt = 0, i;
    for(i = 1;i <= n; ++i)
    {
        if(dis[i] - pre >= tps) pre = dis[i];
        else ++cnt; 
    }
    if(cnt > m || l - pre < tps)return 0;
    return 1;
}
int main()
{
  // freopen("in.txt","r",stdin);
    scanf("%d%d%d",&l,&n,&m);
    for(int i = 1;i <= n;++i)scanf("%d",&dis[i]);
    int L = 0, R = l, ans = 0;
    while(L <= R)
    {
        int mid = (L + R) >> 1;
        if(check(mid)) ans = mid, L = mid + 1;
        else R = mid - 1;
    }
    printf("%d",ans);
    return 0;
}
posted @ 2018-08-30 21:01  EM-LGH  阅读(129)  评论(0编辑  收藏  举报