N one dimensional kingdoms are represented as intervals of the form [ai , bi] on the real line.
A kingdom of the form [L, R] can be destroyed completely by placing a bomb at a point x on the real line if L
≤ x ≤ R.
Your task is to determine minimum number of bombs required to destroy all the one dimensional kingdoms.
Input
- First line of the input contains T denoting number of test cases.
- For each test case, first line contains N denoting the number of one dimensional kingdoms.
- For each next N lines, each line contains two space separated integers ai and bi.
Output
For each test case , output an integer denoting the minimum number of bombs required.
Constraints
Subtask 1 (20 points) : 1 ≤ T ≤ 100 , 1 ≤ N ≤ 100 , 0 ≤ ai ≤ bi ≤500
Subtask 2 (30 points) : 1 ≤ T ≤ 100 , 1 ≤ N ≤ 1000 , 0 ≤ ai ≤ bi ≤ 1000
Subtask 3 (50 points) : 1 ≤ T ≤ 20 , 1 ≤ N ≤ 105, 0 ≤ ai ≤ bi ≤ 2000
Example
Input: 1 3 1 3 2 5 6 9 Output: 2
Explanation
There are three kingdoms [1,3] ,[2,5] and [6,9]. You will need at least 2 bombs
to destroy the kingdoms. In one of the possible solutions, you can place two bombs at x = 2 and x = 6 .
http://www.codechef.com/problems/ONEKING
public class Kingdows {
public static void main(String[] args) {
Scanner cin = new Scanner(
Kingdows.class.getResourceAsStream("Line.txt"));
int t,n;//t表示数据组数,n表示一维数组的个数
t = cin.nextInt();
while(t-->0){
n = cin.nextInt();
Section[] section = new Section[n];
Kingdows kindows = new Kingdows();
ArrayList<Section> MergeSec = new ArrayList<Section>();
for(int i = 0;i<section.length;i++){
int x = cin.nextInt();
int y = cin.nextInt();
section[i] = new Section(x,y);
}
kindows.SecQuickSort(section, 0, n-1);//排序
kindows.mergeSec(section, n, MergeSec);//数组取交集
for(int i = 0;i<MergeSec.size();i++){
System.out.println(MergeSec.get(i).x);//x值即为所需炸弹所在数轴
}
System.out.println(MergeSec.size());//摧毁整个一维王国所需总共的炸弹数量
}
}
/**
* 实体类
* x表示ai
* y表示bi
* @author Administrator
*
*/
static class Section {
int x, y;
public Section(int x, int y) {
super();
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + "," + y + "]";
}
}
/**
* 对一维数组以x为准进行从小到大排序
*
* @param secs
* @param low
* @param high
*/
private void SecQuickSort(Section[] secs, int low, int high){
int i = low;
int j = high;
Section temp = secs[low];
while(i < j){
while(i < j && temp.x < secs[j].x)
j--;
if(i < j){
secs[i] = secs[j];
i++;
}
while(i < j && temp.x > secs[i].x)
i++;
if(i < j){
secs[j] = secs[i];
j--;
}
}
secs[i] = temp;
if(low < i)
SecQuickSort(secs, low, i-1);
if(high > j)
SecQuickSort(secs, j+1, high);
}
/**
* 对排序后的数组取交集
* @param secs
* @param n
* @param MergeSec
*/
private void mergeSec(Section[]secs, int n, ArrayList<Section> MergeSec){
Section temp = secs[0];
//冒泡排序算法依次比较交集取值x值最大,y值最小
for(int i = 1; i < n; i++){
if(temp.y >= secs[i].x){
temp.x = secs[i].x;
if(temp.y >= secs[i].y){
temp.y = secs[i].y;
}
}
else if(temp.y < secs[i].x){
MergeSec.add(temp);
temp = secs[i];
}
}
MergeSec.add(temp);
}
}
参考链接:http://blog.csdn.net/cacyth/article/details/45245583