-
Notifications
You must be signed in to change notification settings - Fork 3
/
copyBooksII.cpp
71 lines (67 loc) · 1.66 KB
/
copyBooksII.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
using namespace std;
class Solution {
public:
/**
* @param n: an integer
* @param times: a vector of integers
* @return: an integer
*/
int copyBooksII(int n, vector<int> ×) {
// write your code here
if(n == 0){
return 0;
}
int k = times.size();
if(k == 0){
return numeric_limits<int>::max();
}
else if (k == 1){
//只有一个抄书员
return times[0] * n;
}
//f[i][j] 表示i个人抄j本书的最小耗时
vector<vector<int> > f(k+1, vector<int>(n+1, 0));
for(int j = 1; j <= n; j++){
f[0][j] = numeric_limits<int>::max();
}
for(int i = 1; i <= k; i++){
for(int j = 1; j <= n; j++){
int minTime = numeric_limits<int>::max();
for(int x = 0; x <= j; x++){
minTime = min(minTime,
max(f[i-1][x], (j - x) * times[i-1]));
}
f[i][j] = minTime;
}
}
return f[k][n];
}
};
int main(){
Solution sol;
int n = 0;
ifstream input("6.in", ios::in);
input>>n;
cout <<"n = "<< n <<endl;
input.seekg(input.tellg()+(long int)3);
vector<int> times;
while(!input.eof()){
int x = 0;
input >> x;
if(x == 0)
break;
times.push_back(x);
input.seekg(input.tellg()+(long int)1);
}
input.close();
for(int j = 0; j < times.size(); j++){
cout << j << ":"<<times[j]<<endl;
}
cout <<"Starting working \n"<<endl;
cout << sol.copyBooksII(n, times)<<endl;
return 0;
}