-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11413.java
72 lines (50 loc) · 1.18 KB
/
11413.java
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
/*
* UVa 11413: Fill the Containers
*
* Problem Statement: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2408
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static ArrayList<Integer> vessels;
public static int n, m, min, minIndex;
public static boolean tryThis(int x) {
int max = Integer.MIN_VALUE;
int inUse = 1;
int current = 0;
for (int v : vessels) {
max = Math.max(max, v);
current += v;
if (current > x) {
current = v;
inUse++;
}
}
if (max > x)
return false;
return inUse <= m;
}
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
n = scan.nextInt();
m = scan.nextInt();
vessels = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
vessels.add(scan.nextInt());
}
int l = 0;
int r = (int) 1e9 + 5;
for (int i = 0; i < 60; ++i) {
if (tryThis((l + r) / 2)) {
r = (l + r) / 2;
} else {
l = (l + r) / 2;
}
}
System.out.println(r);
}
scan.close();
}
}