码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode "Wood Cut"

时间:2015-10-07 12:12:05      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

Binary search. Please not data types and some details.

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)
    {
        long long sum = 0;
        for(auto v : L) sum += v;
        if(sum < k) return 0;
        
        long long a = 0, b = *max_element(L.begin(), L.end());
        long long ret = a;
        while(a <= b)
        {
            long long m = (a + b) / 2;

            long long mk = 0;
            for(auto l : L) mk += l / m;
            
            if(mk < k)
            {
                b = m - 1;
            }
            else if (mk >= k)
            {
                ret = max(ret, m);
                a = m + 1;
            }
        }
        return ret;
    }
};

 

LintCode "Wood Cut"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4858457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!