-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy_books.cpp
More file actions
75 lines (70 loc) · 2.37 KB
/
copy_books.cpp
File metadata and controls
75 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
using namespace std;
/**
* Describe: Given an array A of integer with size of n( means n books and
* number of pages of each book) and k people to copy the book. You must
* distribute the continuous id books to one people to copy. (You can give book
* A[1],A[2] to one people, but you cannot give book A[1], A[3] to one people,
* because book A[1] and A[3] is not continuous.) Each person have can copy one
* page per minute. Return the number of smallest minutes need to copy all the
* books.
*/
class Solution {
public:
int copyBooks(vector<int> &pages, int k) {
if (pages.size() == 0) {
return 0;
}
int len = pages.size();
if (len <= k) {
return *max_element(pages.begin(), pages.end());
}
// dp[x][y] represent the minimal value for x people process the
// first y books.
vector<vector<int>> dp(k + 1, vector<int>(len + 1, 0));
int sum = 0;
int idx;
int pre;
int cur_max;
for (int i = 1; i <= k; ++i) {
idx = i;
pre = i - 1;
sum = 0;
cur_max = dp[i - 1][pre];
while (idx <= len) {
sum += pages[idx - 1];
// when the dp[i - 1][pre + 1] less than sum, and we can make
// sure that the dp[i - 1][pre + 1] is the minimal value for
// dp[i][idx], because when we backtract the sum will increase
// and greater than dp[i - 1][pre + 1], when we forward the pre
// index, the dp[i - 1][pre + 1 + x] will greater than current
// value.
while (i != 1 && pre < idx - 1 && dp[i - 1][pre + 1] < sum) {
++pre;
sum -= pages[pre - 1];
}
// current max is the maximum value of the three
cur_max = max(max(dp[i - 1][pre], sum), cur_max);
dp[i][idx] = cur_max;
++idx;
}
}
return dp[k][len];
}
};
int main() {
Solution so;
int n, k;
vector<int> test;
while (cin >> n) {
cin >> k;
test = vector<int>(n, 0);
for (auto &ele : test) {
cin >> ele;
}
auto re = so.copyBooks(test, k);
cout << "result: " << re << endl;
}
return 0;
}