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暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
随机推荐
- idea破解码
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- 解决Robotium测试用例crash问题
今天遇到一个棘手的问题 用robotium框架真机测试客户端时 跑到一半会crash 搜了一堆资料终于解决了 我的程序引起crash主要原因有两个: 1.用Robotium测试框架跑多个用例(写在同一 ...
- Day1_PHP快速入门
本人知识背景:行业软件C/C++开发两年经验,了解PHP, 所以学习日志偏向记录PHP相对于C的特性 测试环境:EasyPHP13.1 Day 1 学习时间:3小时 1. HTML触发PHP HTML ...
- iOS禁用部分文件ARC
TARGETS的build Phases中的Compile Source里修改文件备注文件参数设定: 增加-fobjc-arc来使单个文件 支持ARC,或者添加-fno-objc-arc使单个文件不支 ...
- canvas学习笔记(一)-认识canvas
canvas是html5新增的一个标签,主要用于图形的绘制.我们可以把它理解为是浏览器给我们提供了一个画板,至于要绘制怎样的画卷,就看神笔马良你的主意了.而在canvas上绘制图形使用的笔,就是js了 ...
- ZOJ1100 状压DP +深搜
记得做过类似于这类题目是能够用组合数学方法来解决的,可惜淡忘了,也找不到了,看了网上的也有人提到过能够用组合公式解决,但是没人做,都是用了状压DP的方法,这个状压非常难讲清楚吧,推荐两篇 第一遍大体看 ...
- (转)130道ASP.NET面试题
130道ASP.NET面试题 转自http://blog.csdn.net/kingmax54212008/article/details/2021204 1. 简述 private. protect ...
- 错误提示:在此上下文中不允许使用名称 "***"。有效表达式包括常量、 常量表达式和变量(在某些上下文中),不允许使用列名。
出现这种情况的原因,是因为在SQL语句的编写格式不正确. 事例展示: 错误: string sql = "insert into person ([name], sex, salary) v ...
- c# session总结
C# 中对 Session 的“(string)”.“.ToString()”与“Convert.ToString”用法笔记 在实际操作当中,我们经常会遇到将 Session 的值转为 String ...
- webpack和webpack-dev-server的区别
第一: webpack只是构建 webpack-dev-server除了构建,还提供web服务 第二:webpack.config.json的路径参数 显然,entry都一样,因为都要知道需要构建 ...