(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. nginx File not found 错误分析与解决方法

    使用php-fpm解析PHP,出错提示如下:"No input file specified","File not found",原因是php-fpm进程找不到 ...

  2. Dreamweaver中打开CodeSmith文件

    电脑环境:Windows2008+Dreamweaver 8英文版本 问题描述:Dreamweaver中默认打开文档时不支持打开CodeSmith模板文件对应的.cst后缀名文件,截图如下: 解决步骤 ...

  3. mysql关联更新

    update tb_sdd_info a,tb_bnm_evian_info b set a.username=b.username where a.username=b.memberno and  ...

  4. 通过案例掌握Spring 管理事务的步骤及配置

    案例描述  通过完成生成订单业务,掌握事务处理.  需要d_order表和d_item表  订单生成时的业务逻辑:向d_order插入1条数据的同时,向t_item中插入若干条数据  这就是一个独立的 ...

  5. dataset 用法(2)

    1.为DataTable添加列 (1)添加列 DataTable  tbl = ds.Tables.Add("User"); DataColumn col =tbl.Columns ...

  6. EF(ServerFirst)执行存储过程实例1(带输出参数)

    1.不含动态sql.带输出参数存储过程调用实例 a.存储过程代码: b.EF自动生成代码(包括对应ObjectResult的实体模型): c.调用存储过程代码实例:  总结: ObjectParame ...

  7. 解决Eclipse无法打开“Failed to load the JNI shared library”(转)

    一般说来,新购笔记本会预装64位的windows系统,而在网上下载软件时,32位会优先出现在页面中(现在来说是这个情况,但我认为未来64位会越来越普及).如果你是64位的系统,却安装了32位的JDK, ...

  8. PL/SQL编程要点和注意点

    http://www.itpub.net/thread-1560427-3-1.html 1. 非关键字小写,关键字大写,用下划线分隔,用前缀区分变量与表列名.不强求变量初始值.2. 永远只捕获可预测 ...

  9. sizeof()的用法

    机器平台:X86_64 处理器 操作系统:Red Hat 4.1.2-14 编译器: gcc version 4.1.2 20070626 Size of char is:               ...

  10. IO库 8.3

    题目:什么情况下,下面的while循环会终止? while(cin >> i) /* ... */ 解答:当读取发生错误时上述while循环会终止.比如i是整形,却输入非整形的数:输入文件 ...