首页 > 其他 > 详细

CareerCup Divide n cakes to k different people

时间:2014-03-02 08:54:34      阅读:400      评论:0      收藏:0      [点我收藏+]

In a party there are n different-flavored cakes of volume V1, V2, V3 ... Vn each. Need to divide them into K people present in the 
party such that 
- Each member of party gets equal volume of cake (say V, which is the solution we are looking for) 
- A given member should get a cake of single flavour only i.e. You cannot distribute parts of different flavored cakes to same 
member. 
- Minimum volume of cake gets wasted after distribution so that means a maximum distribution policy


-------------------------------------------------------------------------------------


Binary Search: We assume the volumes are int. If not, we should define an epi.


class Solution {
 public:
  bool isDivided(vector<int>& vi, int K, int v) {
     
  }
  int getVol(vector<int>& vi, int K) {
    int sum = 0, l = 0, r = 0, maxVol = 0, mid;
    sort(vi.begin(),vi.end(),greater<int>);
    for (int i = 0; i < vi.size(); ++i)
      sum += vi[i];
    l = vi[0] / K;
    r = sum / K;
    while (l <= r) {
      mid = (l + r) / 2;
      if (isDivided(vi, K, mid)) {
        if (mid > maxVol) {
          maxVol = mid;
          l = mid + 1;
        }
        else 
          r = mid - 1;
      }
      else
        r = mid - 1;
    }
    return maxVol;
  }
}


CareerCup Divide n cakes to k different people,布布扣,bubuko.com

CareerCup Divide n cakes to k different people

原文:http://blog.csdn.net/taoqick/article/details/20212403

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!