The bustling town of Siruseri has just one sports stadium. There are a number of schools, colleges, sports associations, etc. that use this stadium as the venue for their sports events.
Anyone interested in using the stadium has to apply to the Manager of the stadium indicating both the starting date (a positive integer S) and the length of the sporting event in days (a positive integer D) they plan to organise. Since these requests could overlap it may not be possible to satisfy everyone.
It is the job of the Manager to decide who gets to use the stadium and who does not. The Manager, being a genial man, would like to keep as many organisations happy as possible and hence would like to allocate the stadium so that maximum number of events are held.
Suppose, for example, the Manager receives the following 4 requests:
Event No. | Start Date | Length |
1 | 2 | 5 |
2 | 9 | 7 |
3 | 15 | 6 |
4 | 9 | 3 |
He would allot the stadium to events 1, 4 and 3. Event 1 begins on day 2 and ends on day 6, event 4 begins on day 9 and ends on day 11 and event 3 begins on day 15 and ends on day 20. You can verify that it is not possible to schedule all the 4 events (since events 2 and 3 overlap and only one of them can get to use the stadium).
Your task is to help the manager find the best possible allotment (i.e., the maximum number of events that can use the stadium).
Input format
The first line of the input will contain a single integer N (N 100000) indicating the number of events for which the Manager has received a request. Lines 2,3,...,N+1 describe the requirements of the N events. Line i+1 contains two integer Si and Di indicating the starting date and the duration of event i. You may assume that 1 Si 1000000 and 1 Di 1000.
Output format
Your output must consist of a single line containing a single integer M, indicating the maximum possible number of events that can use the stadium.
Example:
Sample input:
4
2 5
9 7
15 6
9 3
Sample output:
3
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int i, n = 0;
try {
n = Integer.parseInt(reader.readLine());
int[][] inputArray = new int[n][2];
for (i = 0; i < n; i++) {
String[] temp = reader.readLine().split(" ");
inputArray[i][0] = Integer.parseInt(temp[0]);
inputArray[i][1] = inputArray[i][0] + Integer.parseInt(temp[1]);
}
Arrays.sort(inputArray, new comp());
int prevEndDate = inputArray[0][1];
int maxEvents = 1;
for (i = 1; i < n; i++) {
if (inputArray[i][0] > prevEndDate) {
maxEvents++;
prevEndDate = inputArray[i][1];
}
}
System.out.println(maxEvents);
} catch (Exception e) {
System.out.print(e);
}
}
}
class comp implements Comparator {
int s1[] = new int[2];
int s2[] = new int[2];
public int compare(Object a1, Object a2) {
s1 = (int[]) a1;
s2 = (int[]) a2;
if (s1[1] < s2[1])
return -1;
else if (s1[1] > s2[1])
return 1;
else if (s1[0] < s2[0])
return -1;
else if (s1[0] > s2[0])
return 1;
else
return 0;
}
}