zoukankan      html  css  js  c++  java
  • [NOIP 2015] 跳石头

    [题目链接]

              http://uoj.ac/problem/148

    [算法]

             二分答案,时间复杂度O(NlogL)

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 50010
    typedef long long ll;
    
    ll i,L,N,M,l,r,mid,ans;
    ll dis[MAXN];
    
    template <typename T> inline void read(T &x)
    {
            ll f = 1; x = 0;
            char c = getchar();
            for (; !isdigit(c); c = getchar())
            {
                    if (c == '-') f = -f;
            }
            for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
            x *= f;
    }
    inline bool check(ll d)
    {
            ll i,c = 0,cnt = 0;
            bool flag = false;
            for (i = 1; i <= N; i++)
            {
                    if (dis[i] - dis[c] >= d) c = i;        
                    else cnt++;
                    if (cnt > M) return false;
            }
            if (L - dis[c] < d) cnt++;
            if (cnt <= M) return true;
            return false;
    }
    
    int main() 
    {
            
            read(L); read(N); read(M);
            dis[0] = 0;
            for (i = 1; i <= N; i++) read(dis[i]);
            l = 1; r = 1e15;
            while (l <= r)
            {
                    mid = (l + r) >> 1;
                    if (check(mid))
                    {
                            l = mid + 1;
                            ans = mid; 
                    } else r = mid - 1;
            }
            printf("%lld
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    SpringIOC的小例子
    java中递归实现复制多级文件夹
    快速排序和几种简单排序
    Oracle面试的基本题
    多态的两个小例子
    单例模式
    内部类与匿名内部类
    C#
    C#
    C#
  • 原文地址:https://www.cnblogs.com/evenbao/p/9470884.html
Copyright © 2011-2022 走看看