-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCopyingBooks.cpp
67 lines (59 loc) · 1.49 KB
/
CopyingBooks.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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
long long getScribes(vector<long long>& books, long long nPages);
void printScribesBooks(vector<long long>& books, long long curIdx, long long nScribes, long long K);
int main() {
long long T;
cin >> T;
while(T--) {
long long N, K, sumP = 0;
cin >> N >> K;
vector<long long> books(N);
for(long long i = 0; i < N; i++) {
cin >> books[i];
sumP += books[i];
}
long long start = 0, end = sumP;
while(start < end) {
long long mid = (start + end)/2;
if(getScribes(books, mid) <= K)
end = mid;
else
start = mid+1;
}
printScribesBooks(books, N-1, end, K);
cout << endl;
}
return 0;
}
long long getScribes(vector<long long>& books, long long nPages) {
long long nScribes = 1, curSum = 0;
for(long long i = 0; i < books.size(); i++) {
if(books[i] > nPages) {
nScribes = INT_MAX;
break;
}
if(curSum + books[i] > nPages) {
nScribes++;
curSum = 0;
}
curSum += books[i];
}
return nScribes;
}
void printScribesBooks(vector<long long>& books, long long curIdx, long long nScribes, long long K) {
long long curSum = 0, i;
for( i = curIdx; i >= 0; i--) {
if(curSum + books[i] > nScribes || i+1 == K-1) {
printScribesBooks(books, i, nScribes, K-1);
break;
}
curSum += books[i];
}
if(i >= 0)
cout << "/ ";
for(i = i+1; i <= curIdx; i++)
cout << books[i] << " ";
}