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暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
随机推荐
- mongoose 查询子文档的方法
{ "__v": 1, "_id": "538f5f0f6195a184108c8bd8", "title": &quo ...
- Swift初体验(三)
/*******************************************************************************/ // 协议 protocol Des ...
- 1.对于.NET的初步理解和介绍
好久没写博客了,最近心情比较low,不知道为什么.很流行的一个问题叫做:如果你明天就挂了,那么你最后悔的事情将会是什么.我想了两个月,答案是不知道,无所谓.这样不好,那这个问题先放一边吧,我们开始这一 ...
- SQL Server 2012学习笔记 2 Server Core中命令行安装SQL
Setup.exe /qs /ACTION=Install /FEATURES=SQLEngine,Replication /INSTANCENAME=MSSQLSERVER /SQLSVCACCOU ...
- 创建XML文件
//创建XML文件 XmlDocument xmldoc = new XmlDocument(); XmlText xmltext; ...
- Android Sdk 国内镜像下载地址
大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...
- 原生js写的一个当前年份日期星期和时间的显示
话不多说,所有代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- codeforces 632D. Longest Subsequence 筛法
题目链接 记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码. #include <iostream> #include <vector> #include < ...
- hdu 3397 Sequence operation 线段树
题目链接 给出n个数, 每个数是0或1, 给5种操作, 区间变为1, 区间变为0, 区间0,1翻转, 询问区间内1的个数, 询问区间内最长连续1的个数. 需要将数组开成二维的, 然后区间0, 1翻转只 ...
- less做个径向菜单
在慕课网发现了一个有意思的课程,叫 数学知识在CSS动画中的应用 .用到的数学知识是如何计算圆上每个点的坐标.统一名称,中间的菜单叫触发菜单,四周发散的菜单叫子菜单, 效果预览 慕课网通过jquery ...