LintCode "Copy Books"
Classic DP. The initial intuitive O(k*n^2) solution is like this:
class Solution {
public:
/**
* @param pages: a vector of integers
* @param k: an integer
* @return: an integer
*/
int copyBooks(vector<int> &pages, int k) {
size_t n = pages.size();
if(k > n)
{
return *max_element(pages.begin(), pages.end());
}
// Prefix Sums
vector<long long> psum(n);
for(int i = ; i < n; i ++)
psum[i] = i == ? pages[i] : (psum[i - ] + pages[i]);
// DP
vector<vector<long long>> dp(n + , vector<long long>(k + , INT_MAX));
for(int i = ; i <= n; i ++)
dp[i][] = psum[i - ];
for(int i = ; i <= k; i ++) // person
for(int b = i; b <= n; b ++) // book
for(int c = i-; c < b; c ++) // prev book
{
long long last = dp[c][i - ];
long long cur = psum[b-] - psum[c - ];
dp[b][i] = min(dp[b][i], max(cur, last));
}
return dp[n][k];
}
};
O(nk): http://sidbai.github.io/2015/07/25/Copy-Books/Point above:
long long last = dp[c][i - 1];
long long cur = psum[b-1] - psum[c - 1];
min(dp[b][i], max(cur, last));
dp[c][i-1] is mono-inc by c, cur is mono-dec. min(.., max(cur,last)) is V-like in 2D plane. So we can use 2-pointers to find the bottom of the V!
Or, binary search with O(nlg(sum/k)): https://github.com/kamyu104/LintCode/blob/master/C++/copy-books.cpp
LintCode "Copy Books"的更多相关文章
- [LintCode] Copy Books 复印书籍
Given an array A of integer with size of n( means n books and number of pages of each book) and k pe ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- Copy Books
Description Given n books and the i-th book has pages[i] pages. There are k persons to copy these bo ...
- Copy Books II
Description Given n books and each book has the same number of pages. There are k persons to copy th ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- Leetcode Lect3 二分法总结
二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...
- postgresql批量备份和恢复数据表
备份数据库:pg_dump -h localhost -U root demo02 > /home/arno/dumps/demo02.bak 恢复数据库:psql -h localhost - ...
随机推荐
- USB相关的网络资料
相关资源连接: USB官网:http://www.usb.org/home <USB in a NutShell>: http://www.beyondlogic.org/usbnutsh ...
- Android Bitmap OOM处理
public int calculateInSampleSize(BitmapFactory.Options option, int rWidth, int rHeight) { ...
- DPM(Deformable Parts Model)--原理(一)(转载)
DPM(Deformable Parts Model) Reference: Object detection with discriminatively trained partbased mode ...
- Sprint第二个冲刺(第一天)
因为人员变动关系,我们的博客推迟了两天发布,希望老师能够谅解. 现在“广商百货”团队项目的新的团队成员组成为:董婷婷(组长).容杰龙.卓炜杰.袁文洪和吴建明 在经过第一轮和几天的休息,现在我们准备开始 ...
- Websphere发布时遇到的问题
在发布时遇到了data source配置的问题,搞了好久没搞定,最后问题出现在 JDBC providers那边,Implementation class name 改成: com.ibm.db2.j ...
- 1-3-1 关于API
主要内容:API函数及其相关内容的介绍.Windows编程相关基础知识介绍 1.API函数的概念 <1>API(Application Programming interface),即应用 ...
- timus 1106 Two Teams(二部图)
Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...
- hihoCoder#1015 : KMP算法 (KMP模板)
代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector ...
- 关于VC、MFC和ACCESS的一些使用问题
最近在用VC.MFC和ACCESS开发一些小工具. 由于操作系统和开发工具以及数据库版本都升级了,和当年有一些区别了(我这是有多老了--fuck--),遇到一些问题,贴在下面: 1,用什么连接AC ...
- Codeforces Round #339 Div.2 A - Link/Cut Tree
第一次正式参加常规赛想想有些小激动的呢 然后第一题就被hack了 心痛 _(:зゝ∠)_ tle点在于越界 因此结束循环条件从乘变为除 done //等等 这题没过总评 让我静静........ // ...