-
Notifications
You must be signed in to change notification settings - Fork 9
/
moving_books.cpp
98 lines (80 loc) · 2.27 KB
/
moving_books.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Strategy:
// Sort strengths and weights in ascending order
// For i = 1..n:
// - Take all boxes that are too heavy for everyone before, but not too heavy for person i
// - If person i carries less than the person carrying the most, take over boxes
void solve() {
int n, m;
cin >> n >> m;
// Read strengths
vector<int> s(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
// Read weights
vector<int> w(m);
for (int i = 0; i < m; ++i) {
cin >> w[i];
}
// Sort strengths and weights
sort(s.begin(), s.end());
sort(w.begin(), w.end());
// If the heaviest box is to heavy for the strongest person, it is impossible
if (s[n - 1] < w[m - 1]) {
cout << "impossible" << endl;
return;
}
// Keep track what the maximum amount is someone carries
int maxBoxes = 0;
int j = 0;
vector<int> numPeople(m + 1, 0); // How many people carry exactly j boxes
for (int i = 0; i < n; ++i) {
// Slide over boxes for counting
int count = 0;
while (j < m && w[j] <= s[i]) {
++j;
++count;
}
numPeople[count]++;
// A new maximum
if (count >= maxBoxes || i == 0) {
maxBoxes = count;
}
// Person i can take over boxes of other people
else {
// While person i has some spare capacity
while (count < maxBoxes) {
// Take one box form each person that is already carrying the maxBoxes
// Make sure not to take on too many boxes
int delta = min(numPeople[maxBoxes], maxBoxes - count);
// one person now carries delta additional boxes
numPeople[count] -= 1;
numPeople[count + delta] += 1;
// delta people now carry one box less
numPeople[maxBoxes] -= delta;
numPeople[maxBoxes - 1] += delta;
// Update count for current person
count += delta;
// If there is no one left at full capacity, reduce capacity
if (numPeople[maxBoxes] == 0) {
maxBoxes--;
}
}
}
}
// Calculate time
int time = 3 * maxBoxes - 1;
cout << time << endl;
}
int main() {
ios_base::sync_with_stdio(false);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}