File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.InputStreamReader ;
4+ import java.util.Arrays ;
5+ import java.util.StringTokenizer ;
6+
7+ public class Main {
8+
9+ static int n, c;
10+ static int [] house;
11+
12+ public static void main (String [] args ) throws Exception {
13+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
14+ StringTokenizer st;
15+
16+ st = new StringTokenizer (br. readLine());
17+ n = Integer . parseInt(st. nextToken());
18+ c = Integer . parseInt(st. nextToken());
19+
20+ house = new int [n];
21+ for (int i = 0 ; i < n; i++ ) {
22+ house[i] = Integer . parseInt(br. readLine());
23+ }
24+
25+ Arrays . sort(house);
26+
27+ int low = 1 ;
28+ int high = house[n - 1 ] - house[0 ];
29+ int answer = 0 ;
30+
31+ while (low <= high) {
32+ int mid = (low + high) / 2 ;
33+
34+ if (canInstall(mid)) {
35+ answer = mid;
36+ low = mid + 1 ;
37+ } else {
38+ high = mid - 1 ;
39+ }
40+ }
41+
42+ System . out. println(answer);
43+ }
44+
45+ static boolean canInstall (int distance ) {
46+ int count = 1 ;
47+ int lastInstalled = house[0 ];
48+
49+ for (int i = 1 ; i < n; i++ ) {
50+ if (house[i] - lastInstalled >= distance) {
51+ count++ ;
52+ lastInstalled = house[i];
53+ }
54+ }
55+
56+ return count >= c;
57+ }
58+ }
59+
60+ ```
You can’t perform that action at this time.
0 commit comments