(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的更多相关文章

  1. 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 ...

  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. 在非gui线程使用QMessageBox

    最近我写项目的时候遇到一个奇怪的需求,要在工作线程内,根据某个情况弹出一个MessageBox 但是Qt提供的MessageBox只可以在gui线程(主线程)使用,于是我就对QMessageBox封装 ...

  2. Centos 升级MySQL版本或者Yum安装Mysql5.6

    Centos 升级MySQL版本或者Yum安装Mysql5.6 1.从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/Cen ...

  3. perl 跨行匹配;

    <pre name="code" class="html"><pre name="code" class="ht ...

  4. IE8 多进程问题

    IE8的一个重要特性就是每个Tab(选项卡)在独立的进程中运行,我们称之为LCIE(Loosely-Coupled IE). 所以大家在升级到IE8之后会发现资源管理器里面有两个或者多个iexplor ...

  5. Windows7中搭建Android x86_64及armv8-a操作步骤

    1.        从https://developer.android.com/tools/sdk/ndk/index.html 下载android-ndk-r10d-windows-x86_64. ...

  6. Rabbit and Grass(杭电1849)(尼姆博弈)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. 关于Apacheserver的訪问控制

    Apache的訪问控制指对不论什么资源的不论什么方式的訪问控制. 一.基于主机或者IP地址的控制 这样的訪问控制基于訪问者的主机名或者IP地址,通过使用 Deny 和 Allow 指令.实现同意或者禁 ...

  8. ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象

    第一种:导出gridVIEW中的数据,用hansTABLE做离线表,将数据库中指定表中的所有数据按GRIDVIEW中绑定的ID导出 只能导出数据不能去操作相应的EXCEl表格,不能对EXCEL中的数据 ...

  9. 用C++写一个简单的发布者

    节点是一个可执行程序,它连接到了ROS的网络系统中.我们将会创建一个发布者,也就是说话者节点,它将会持续的广播一个信息. 改变目录到之前所建立的那个包下: cd ~/catkin_ws/src/beg ...

  10. Struts2知识总结

    整篇参考:http://blog.csdn.net/zq9017197/article/details/5958627 要搞清楚以下几点: 1.Struts2是什么?它的运行原理是什么? 2.Stru ...