-
Notifications
You must be signed in to change notification settings - Fork 0
/
WoodCutter.java
30 lines (29 loc) · 947 Bytes
/
WoodCutter.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
import java.io.*;
import java.util.*;
public class WoodCutter {
public static void main(String[] args) throws IOException {
BufferedReader f =new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(f.readLine());
int[] trees = new int[n];
int[] height = new int[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(f.readLine());
trees[i] = Integer.parseInt(st.nextToken());
height[i] = Integer.parseInt(st.nextToken());
}
int count = Math.min(n, 2);
boolean fellRight = false;
for (int i = 1; i + 1 < n; i++) {
if (trees[i] - height[i] > trees[i - 1] &&
(!fellRight || trees[i -1] + height[i-1] < trees[i] - height[i])) {
count++;
fellRight = false;
}
else if (trees[i] + height[i] < trees[i + 1]) {
count++;
fellRight = true;
}
}
System.out.println(count);
}
}