forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 11413 - Fill the Containers.cpp
56 lines (52 loc) · 1.27 KB
/
UVa 11413 - Fill the Containers.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
#include <iostream>
#include <vector>
using namespace std;
// Can the vessels fill all the m containers, each has capacity C?
bool FillAllContainers(const vector<int> &vessels,
int m,
int C)
{
int container = 1;
int capacity = C;
for (int i = 0; i < vessels.size(); ++i)
{
// No container can contain so much milk.
if (vessels[i] > C)
return false;
if (vessels[i] > capacity)
{
// Already m containers are filled.
if (container == m)
return false;
++container;
capacity = C;
}
capacity -= vessels[i];
}
return true;
}
int main()
{
int n, m;
while (cin >> n >> m)
{
vector<int> vessels(n);
for (int i = 0; i < n; ++i)
cin >> vessels[i];
// The capacity 1<=c<=1000000000.
int L = 1, U = 1000000000, C = 0;
while (L <= U)
{
int mid = (L + U) / 2;
if (FillAllContainers(vessels, m, mid))
{
C = mid;
U = mid - 1;
}
else
L = mid + 1;
}
cout << C << endl;
}
return 0;
}