(http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html)

This is Part II of the artical: The Painter's Partition Problem. Please read Part I for more background information.

Solution:

Assume that you are assigning continuous section of board to each painter such that its total length must not exceed a predefined maximum, costmax. Then, you are able to find the number of painters that is required, x. Following are some key obervations:

  • The lowest possible value for costmax must be the maximum element in A (name this as lo).
  • The highest possible value for costmax must be the entire sum of A (name this as hi).
  • As costmax increases, x decreases. The opposite also holds true.

Now, the question translates directly into:

  • How do we use binary search to find the minimum of costmax while satifying the condition x=k? The search space will be the range of [lo, hi].
int getMax(int A[], int n)
{
int max = INT_MIN;
for (int i = ; i < n; i++)
{
if (A[i] > max)
max = A[i];
}
return max;
} int getSum(int A[], int n)
{
int total = ;
for (int i = ; i < n; i++)
total += A[i];
return total;
} int getRequiredPainters(int A[], int n, int maxLengthPainter)
{
int total =;
int numPainters = ;
for (int i = ; i < n; i++)
{
total += A[i];
if (total > maxLengthPerPainter)
{
total = A[i];
numPainters++;
}
}
return numPainters;
} int partition(int A[], int n, int k)
{
if (A == NULL || n <= || k <= )
return -; int lo = getMax(A, n);
int hi = getSum(A, n); while (lo < hi)
{
int mid = lo + (hi-lo)/;
int requiredPainters = getRequiredPainter(A, n, mid);
if (requiredPainters <= k)
hi = mid;
else
lo = mid+;
}
return lo;
}

The complexity of this algorithm is O(N log(∑Ai)), which is quite efficient. Furthermore, it does not require any extra space, unlike the DP solution which requires O(kN) space.

The Painter's Partition Problem Part II的更多相关文章

  1. The Painter's Partition Problem Part I

    (http://leetcode.com/2011/04/the-painters-partition-problem.html) You have to paint N boards of leng ...

  2. 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化

    Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...

  3. poj 1681 Painter&#39;s Problem(高斯消元)

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...

  4. 2019年牛客多校第二场 F题Partition problem 爆搜

    题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...

  5. 【搜索】Partition problem

    题目链接:传送门 题面: [题意] 给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]. [题解] 爆搜,C(28,14)*28,其实可以稍加优化, ...

  6. 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举

    https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...

  7. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  8. 2019牛客多校2 F Partition problem(dfs)

    题意: n<=28个人,分成人数相同的两组,给你2*n*2*n的矩阵,如果(i,j)在不同的组里,竞争力增加v[i][j],问你怎么分配竞争力最 4s 思路: 枚举C(28,14)的状态,更新答 ...

  9. 2019牛客多校第二场F Partition problem(暴搜)题解

    题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...

随机推荐

  1. UberX及以上级别车奖励政策(优步北京第一组)

    优步北京第一组: 定义为2015年6月1日凌晨前(不含6月1日)激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机( ...

  2. Android 匿名共享内存C++接口分析

    在上一篇Android 匿名共享内存C接口分析中介绍了Android系统的匿名共享内存C语言访问接口,本文在前文的基础上继续介绍Android系统的匿名共享内存提供的C++访问接口.在C++层通过引入 ...

  3. iOS深入学习 (Block全面分析)

    本文翻译自苹果的文档,有删减,也有添加自己的理解部分. 如果有Block语法不懂的,可以参考fuckingblocksyntax,里面对于Block 为了方便对比,下面的代码我假设是写在ViewCon ...

  4. ceph优秀博文

    ceph官方博文: http://ceph.com/community/blog/ rgw根据rgw用户来分pool存放数据 http://cephnotes.ksperis.com/blog/201 ...

  5. DataReader转泛型

    实体类的字段类型要和数据库一致,不然可能会出现错误. /// <summary> /// DataReader转泛型 /// </summary> /// <typepa ...

  6. 从VS转MyEclipse的15天使用体验

    脱离了VS强大的IDE功能之后,转向MyEclipse,发现很大差别,Java的IDE对比VS感觉弱很多,而且树形没有那么好用,Java里面是以包为主,区别与C#的最大就是,高亮提示关键字,这一点Ja ...

  7. JDK源码学习--String篇(二) 关于String采用final修饰的思考

    JDK源码学习String篇中,有一处错误,String类用final[不能被改变的]修饰,而我却写成静态的,感谢CTO-淼淼的指正. 风一样的码农提出的String为何采用final的设计,阅读JD ...

  8. ACM题目:487-3279

    题目是这样子的 Description Businesses like to have memorable telephone numbers. One way to make a telephone ...

  9. Windows下提升进程权限(转)

    from: http://www.oschina.net/code/snippet_222150_19533 windows的每个用户登录系统后,系统会产生一个访问令牌(access token) , ...

  10. BZOJ 2302: [HAOI2011]Problem c( dp )

    dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...