package com.test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
* 输入:
* 第一行: 一个整数n, 代表之后要输入多少个区间
* 之后, 每行有两个数, 例如: 3 6 其代表区间[3, 6)
* 输入实例:
* 4
* 1 9
* 2 3
* 3 5
* 3 6
*
* 输出:
* 3
* 该输出代表重叠区间个数的最大值
* 上述输入所有可能的重叠为:
* 1 9, 2 3
* 1 9, 3 5
* 1 9, 3 6
* 3 5, 3 6
* 1 9, 3 5, 3 6
* 所以, 重叠区间个数的最大值为3, 即1 9, 3 5, 3 6这三个区间的重合
*/
public class OverlapIntervalCount {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n == 0){
System.out.println(0);
}
Interval[] intervals = new Interval[n*2];
int i = 0;
while (i<n) {
int start = in.nextInt();
int end = in.nextInt();
intervals[i*2] = new Interval(start, 0);
intervals[i*2+1] = new Interval(end, 1);
i++;
}
Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval o1, Interval o2) {
if (o1.val < o2.val){
return -1;
} else if (o1.val > o2.val){
return 1;
} else {
if (o1.type == o2.type){
return 0;
} else {
// 由于类似[3, 5), [5, 6)这种不算是重合区间, 所以在排序时要保证在两个val相同时, type=1要在type=0前
if (o1.type > o2.type){
return -1;
} else {
return 1;
}
}
}
}
});
// 以下代码用于查看排序好的intervals数组中的所有元素
for (int d=0;d<2*n;d++){
System.out.println("type: "+intervals[d].type+", val: "+intervals[d].val);
}
// 核心代码
int max = 1;
int count = 0;
for (int j=0;j<2*n;j++){
if (intervals[j].type == 0){
count++;
max = Math.max(max, count);
} else {
// 说明某个区间已经闭合, 所以总计数应该减少1
count--;
}
}
System.out.println(max);
}
}
class Interval{
public int type;
public int val;
public Interval(int val, int type){
this.type = type;
this.val = val;
}
}