-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeribot.java
139 lines (115 loc) · 2.87 KB
/
Feribot.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Buliga Theodor Ioan
// 323 CA
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class Feribot {
public static void main(final String[] args) throws IOException {
var scanner = new MyScanner(new FileReader("feribot.in"));
// n cars, k ferries
var n = scanner.nextInt();
var k = scanner.nextLong();
// keeping the weight of the cars
var a = new long[n];
for (var i = 0; i < n; i += 1) {
a[i] = scanner.nextLong();
}
// printing the result
try (var printer = new PrintStream("feribot.out")) {
printer.println(solutionFeribot(a, k));
}
}
public static long solutionFeribot(long[] a, long k) {
// setting the length, left and right staring point
long n = a.length, left = 0, right = 0;
for (int i = 0; i < a.length; i++) {
// left as the maximum
left = Math.max(left, a[i]);
// right as the sum of elements
right += a[i];
}
// if there is only one ferry
// I return the greatest value
if (k == 1) {
return right;
}
while (left < right) {
// setting the middle
long mid = left + (right - left) / 2;
boolean ok = true;
long ferries = 1, sum = 0;
// iterating through all the cars
for (int i = 0; i < a.length && ok; i++) {
// calculating the sum
sum += a[i];
// if the sum is over the maximum
if (sum > mid) {
// increasing the number of ferries used
ferries++;
// resetting the sum
// as the current car's weight
sum = a[i];
// if the number of ferries used overtook the
// maximum available
if (ferries > k) {
ok = false;
}
}
}
// if all the ferries did not overtake
// we are searching in the smaller interval
if (ok) {
right = mid;
} else {
// otherwise searching in the greater interval
left = mid + 1;
}
}
return right;
}
// I took this from the other skels
/**
* A class for buffering read operations, inspired from here:
* https://pastebin.com/XGUjEyMN.
*/
private static class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner(Reader reader) {
br = new BufferedReader(reader);
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}