-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocation.cpp
More file actions
66 lines (58 loc) · 1.36 KB
/
allocation.cpp
File metadata and controls
66 lines (58 loc) · 1.36 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
#include <bits/stdc++.h>
// 積載量Pに対して積むことのできる
// 荷物の数を返す
int BaggageAmount(int P, std::vector<int> vecW, int& idx, int n) {
int sum = 0;
int i = 0;
// 積載量P以下の限り
// 順番に荷物を積み続ける
while (true) {
// 積載量Pを超える前に終了
if (sum + vecW[idx+i] > P) break;
if (i == n) break;
sum += vecW[idx+i];
i++;
}
idx += i;
return i;
}
int main()
{
int n = 0;
int k = 0;
int w = 0;
int sum = 0;
std::vector<int> vecW (200000, 0);
// 荷物の個数を取得
std::cin >> n;
// トラックの台数を取得
std::cin >> k;
for (int i = 0; i < n; i++)
{
std::cin >> w;
vecW[i] = w;
}
int P = 0;
int idx = 0;
int left = 0;
// 必要積載量 P の最大値 = 積載量荷物 * 重量の最大値
int right = 100000 * 10000;
int mid = 0;
// 二分木探索で最小となるPを探す
while (right > left) {
sum = 0;
idx = 0;
mid = (right + left) / 2;
for (int i = 0; i < k; i++)
{
sum += BaggageAmount(mid, vecW, idx, n);
}
if (sum < n) {
left = mid + 1;
} else {
right = mid;
}
}
std::cout << right <<"\n";
return 0;
}