/
AngryCows2.java
78 lines (77 loc) · 3 KB
/
AngryCows2.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
import java.util.*;
import java.io.*;
public class AngryCows2 {
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(new BufferedReader(new FileReader("angry.in")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("angry.out")));
int n = scan.nextInt();
int[] locs = new int[n];
for (int i = 0; i < n; i++) {
locs[i] = scan.nextInt();
}
Arrays.sort(locs);
// locs = {0, 2, 6, 9, 11, 12, 14}
// index into locs: 4th bale locs[3] = 9
// don't confuse index into locs with position of bales
// idxStart
// locCurrent
// idxStart = 4 ^
// idxCurrent $
// radius = 2
// locExpl = 9
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// o o o o o o o o
// ^
// $
int maxExplosions = 0;
for (int idxStart = 0; idxStart < n; idxStart++) {
int idxCurrent = idxStart;
int radius = 1;
int rightExplosions = 0;
// advance idxCurrent to the right until we run out of explosions
while (true) {
// establish a loop invariant - something that is true at the start of every loop
// loop invariant: idxCurrent is index of where explosion took place
int locExpl = locs[idxCurrent];
// loop invariant: locCurrent is location of where last explosion took place
// advance within current radius
// explosions will take place up to locCurrent+radius
int loopCount = 0;
while (idxCurrent < n && locs[idxCurrent] <= locExpl + radius) {
idxCurrent++;
loopCount++;
}
// here check for termination
if (idxCurrent == n || loopCount == 1) {
rightExplosions = idxCurrent - idxStart;
break;
}
// idxCurrent - idxStart is # of explosions
idxCurrent--;
radius++;
}
int leftExplosions = 0;
idxCurrent = idxStart;
radius = 1;
while (true) {
int locExpl = locs[idxCurrent];
int loopCount = 0;
while (idxCurrent >= 0 && locs[idxCurrent] >= locExpl - radius) {
idxCurrent--;
loopCount++;
}
if (idxCurrent < 0 || loopCount == 1) {
leftExplosions = idxStart - idxCurrent;
break;
}
idxCurrent++;
radius++;
}
// check total explosions
maxExplosions = Math.max(maxExplosions, leftExplosions + rightExplosions - 1);
}
pw.println(maxExplosions);
scan.close();
pw.close();
}
}