The Painter's Partition Problem Part I
(http://leetcode.com/2011/04/the-painters-partition-problem.html)
You have to paint N boards of lenght {A0, A1, A2 ... AN-1}. There are K painters available and you are also given how much time a painter takes to paint 1 unit of board. You have to get this job done as soon as possible under the constraints that any painter will only paint continues sections of board, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
We define M[n, k] as the optimum cost of a partition arrangement with n total blocks from the first block and k patitions, so
n n-1
M[n, k] = min { max { M[j, k-], ∑ Ai } }
j=1 i=j
The base cases are:
M[, k] = A0
n-1
M[n, ] = Σ Ai
i=0
Therefore, the brute force solution is:
int sum(int A[], int from, int to)
{
int total = ;
for (int i = from; i <= to; i++)
total += A[i];
return total;
} int partition(int A[], int n, int k)
{
if (n <= || k <= )
return -;
if (n == )
return A[];
if (k == )
return sum(A, , n-); int best = INT_MAX;
for (int j = ; j <= n; j++)
best = min(best, max(partition(A, j, k-), sum(A, j, n-))); return best;
}
It is exponential in run time complexity due to re-computation of the same values over and over again.
The DP solution:
int findMax(int A[], int n, int k)
{
int M[n+][k+];
int sum[n+];
for (int i = ; i <= n; i++)
sum[i] = sum[i-] + A[i-]; for (int i = ; i <= n; i++)
M[i][] = sum[i];
for (int i = ; i <= k; i++)
M[][k] = A[]; for (int i = ; i <= k; i++)
{
for (int j = ; j <= n; j++)
{
int best = INT_MAX;
for (int p = ; p <= j; p++)
{
best = min(best, max(M[p][i-], sum[j]-sum[p]));
}
M[j][i] = best;
}
}
return M[n][k];
}
Run time: O(kN*N), space complexity: O(kN).
The Painter's Partition Problem Part I的更多相关文章
- The Painter's Partition Problem Part II
(http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- poj 1681 Painter's Problem(高斯消元)
id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...
- 2019年牛客多校第二场 F题Partition problem 爆搜
题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...
- 【搜索】Partition problem
题目链接:传送门 题面: [题意] 给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]. [题解] 爆搜,C(28,14)*28,其实可以稍加优化, ...
- 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举
https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客多校2 F Partition problem(dfs)
题意: n<=28个人,分成人数相同的两组,给你2*n*2*n的矩阵,如果(i,j)在不同的组里,竞争力增加v[i][j],问你怎么分配竞争力最 4s 思路: 枚举C(28,14)的状态,更新答 ...
- 2019牛客多校第二场F Partition problem(暴搜)题解
题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
随机推荐
- Android4: Write Storage权限问题
原文:Android4: Write Storage权限问题 2.3中声明 <uses-permission android:name="android.permission.WRIT ...
- 一次性关闭所有的Activity
原文:一次性关闭所有的Activity 一次性关闭所有的Activity ActivityManager am = (ActivityManager)getSystemService (Context ...
- WPF中的触发器简单总结
原文 http://blog.sina.com.cn/s/blog_5f2ed5cb0100p3ab.html 触发器,从某种意义上来说它也是一种Style,因为它包含有一个Setter集合,并根据一 ...
- rsyslog ~ 波浪号
<pre name="code" class="html">Using negation can be useful if you would li ...
- sql优化-总结
1.尽量缩小数据范围. 2.能一个sql解决的,坚决不用两条sql.利用case when或decode. select month_id, corppkno, sum(exportSum_new) ...
- 面向对象程序设计-C++_课时16子类父类关系
初始化列表 类名::类名(形参1,形参2,...形参n):数据成员1(形参1),数据成员2(形参2),...,数据成员n(形参n) { ... } 规则1,初始化列表进行数据成员的初始化 规则2,初始 ...
- ubuntu13.04安装SenchaArchitect-2.2无法启动的问题
系统是ubuntukylin-13.04-desktop版本,不知道别的版本有没有这个问题,未测试.SenchaArchitect采用最新版本2.2.2,我安装的是32位的. 具体无法启动的问题如下: ...
- Python的中文编码转换问题
与server进行数据交换时,尤其是数据中含有中文时,要注意中文的编码问题. 要选择server接受的编码方式,否则会造成显示乱码. 经验: 实验室server的数据库,中文用UTF-8编码,但我提交 ...
- My97DaePicker 用js实现文本框日期相减求天数
<tr> <td align="center" style="background-color: #cccccc;font ...
- JavaSE思维导图(一)