woodcut

http://www.lintcode.com/en/problem/wood-cut/#


二分答案,贪心验证,具有单调性


class Solution {
public:
    /**
     *@param L: Given n pieces of wood with length L[i]
     *@param k: An integer
     *return: The maximum length of the small pieces.
     */
    int woodCut(vector<int> L, int k){
        if(L.empty()) return 0;
        LL low=0, high=*max_element(L.begin(), L.end());
        while(low<high){
            if(low+1==high){
                LL ans=0;
                for(auto e: L){
                    ans+=e/high;
                }
                if(ans>=k) return high;
                else return low;
            }
            LL mid=(low+high)/2;
            LL ans=0;
            for(auto e: L){
                ans+=e/mid;
            }
            if(ans>=k)  low=mid;
            else high=mid-1;
        }
        return low;
    }
}S;

introsort C++ STL 没有最坏情况

timsort msort isort 综合

dual pivot qsort JDK7 arrays.sort 里面採用 任然有最坏n^2情况

posted @ 2017-07-13 16:30  lytwajue  阅读(215)  评论(0编辑  收藏  举报