Description
There is given the series of n closed intervals [ai; b i], where i=1,2,...,n. The sum of those intervals may berepresented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.
Task
Write a program which:
.reads from the std input the description of the series of intervals,
.computes pairwise non−intersecting intervals satisfying the conditions given above,
.writes the computed intervals in ascending order into std output
Input
In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.
Output
The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.
Sample Input
5
5 6
1 4
10 10
6 9
8 10
Sample Output
1 4
5 10
package p1089;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
IntervalSet intervalSet = new IntervalSet();
int count = in.nextInt();
for (int i = 0; i < count; i++) {
Interval tmpInterval = new Interval();
tmpInterval.start = in.nextInt();
tmpInterval.end = in.nextInt();
intervalSet.addInterval(tmpInterval);
}
intervalSet.printInfo();
}
private static class Interval {
int start;
int end;
public Interval() {
}
public boolean isOverlap(Interval interval) {
return !(this.start > interval.end || interval.start > this.end);
}
public void merge(Interval interval) {
this.start = Math.min(this.start, interval.start);
this.end = Math.max(this.end, interval.end);
}
public void printInfo() {
System.out.println(start + " " + end);
}
}
private static class IntervalSet {
List<Interval> list = new ArrayList<Interval>();
boolean isOverlap(int position, Interval interval) {
return list.get(position).isOverlap(interval);
}
/**
* return -1 if there is no overlap.
*/
int findOverlapStart(int start, int end, Interval interval) {
if (start > end)
return -1;
if (start == end) {
return isOverlap(start, interval) ? start : -1;
}
int middle = (start + end) / 2;
Interval middleInterval = list.get(middle);
if (middleInterval.isOverlap(interval)) {
return findOverlapStart(start, middle, interval);
} else if (interval.start < middleInterval.start) {
return findOverlapStart(start, middle - 1, interval);
} else {
return findOverlapStart(middle + 1, end, interval);
}
}
int findOverlapEnd(int start, int end, Interval interval) {
if (start > end)
return -1;
if (start == end) {
return isOverlap(start, interval) ? start : -1;
}
// if there are 2 element in the range, we should test it out.
if (start + 1 == end) {
if (isOverlap(end, interval))
return end;
if (isOverlap(start, interval))
return start;
return -1;
}
int middle = (start + end) / 2;
Interval middleInterval = list.get(middle);
if (middleInterval.isOverlap(interval)) {
return findOverlapEnd(middle, end, interval);
} else if (interval.start < middleInterval.start) {
return findOverlapEnd(start, middle - 1, interval);
} else {
return findOverlapEnd(middle + 1, end, interval);
}
}
int findInsertPosition(int start, int end, Interval interval) {
if (start == end) {
return start;
}
int middle = (start + end) / 2;
Interval middleInterval = list.get(middle);
if (middleInterval.start > interval.start) {
return findInsertPosition(start, middle, interval);
} else {
return findInsertPosition(middle + 1, end, interval);
}
}
/**
* find the position to insert.
*/
int findInsertPosition(Interval interval) {
if (interval.start > list.get(list.size() - 1).start)
return list.size();
return findInsertPosition(0, list.size() - 1, interval);
}
/**
* return -1 if there is no overlap.
*/
int findOverlapStart(Interval interval) {
return findOverlapStart(0, list.size() - 1, interval);
}
/**
* return -1 if there is no overlap.
*/
int findOverlapEnd(Interval interval) {
return findOverlapEnd(0, list.size() - 1, interval);
}
void addInterval(Interval interval) {
// 空list情况.
int size = list.size();
if (size == 0) {
list.add(interval);
return;
}
int overlapStart = findOverlapStart(interval);
// There is no overlap.
if (overlapStart == -1) {
int insertPosition = findInsertPosition(interval);
list.add(insertPosition, interval);
return;
}
int overlapEnd = findOverlapEnd(interval);
list.get(overlapStart).merge(interval);
list.get(overlapStart).merge(list.get(overlapEnd));
for (int i = overlapEnd; i > overlapStart; i--) {
list.remove(i);
}
}
void printInfo() {
for (int i = 0; i < list.size(); i++) {
list.get(i).printInfo();
}
}
}
}