-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvention.java
More file actions
46 lines (45 loc) · 1.3 KB
/
Convention.java
File metadata and controls
46 lines (45 loc) · 1.3 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
import java.io.*;
import java.util.*;
public class Convention {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("convention.in"));
String[] temp;
temp = br.readLine().split(" ");
int num_cows = Integer.parseInt(temp[0]);
int num_bus = Integer.parseInt(temp[1]);
int capacity = Integer.parseInt(temp[2]);
temp = br.readLine().split(" ");
ArrayList<Integer> cows = new ArrayList<Integer>();
for (int i = 0; i<num_cows; i++)
cows.add(Integer.parseInt(temp[i]));
int minIndex;
int temp2;
Collections.sort(cows);
int output = binarySearch(0, 1000000000, cows, num_bus, capacity) + 1;
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("convention.out")));
pw.println(output);
pw.close();
br.close();
}
public static int binarySearch(int start, int end, ArrayList<Integer> cows, int num_bus, int capacity)
{
int point = 0;
int mid = (start+end)/2;
int count = 1;
if (start == mid)
return start;
for (int i = 0; i < cows.size(); i++)
{
if (cows.get(i) - cows.get(point) > mid || (i-point) == capacity)
{
count++;
point = i;
}
}
if (count > num_bus)
return binarySearch(mid, end, cows, num_bus, capacity);
else
return binarySearch(start, mid, cows, num_bus, capacity);
}
}