Given n intervals, you should merge all overlapping intervals.
For example, the given intervals are:
[1,3], [2,6], [3,5], [7,9]
you should output:
[1,6], [7,9]
The first line is an integer n(1<=n<=100) indicating the number of intervals. There are n lines follow, each line contain two integer xi,yi (1<=xi<=yi<=10000) that indicate the given interval [xi,yi].
Assume after merge, there are m intervals. You should output m lines, each line contains two integers, xi and yi, which indicate the interval [xi,yi].
It should be guaranteed that for all i < j, yi < xj.
input
4
7 9
2 6
3 5
1 3
output
1 6
7 9
思路:把所有数乘十,然后把个位当做十分位,这样就可以避免类似6到7之间没东西但被合并的情况
#include <iostream>
#include <cstring>
using namespace std;
int arr[100000];
int main()
{
memset(arr,0,sizeof(arr));
int num;
cin >> num;
while(num--){
int l,r;
cin >> l >> r;
for(int i=l*10; i <= r*10; i++)
arr[i]++;
}
for(int i=10; i <= 100000; i++){
if(arr[i]!=0&&arr[i-1]==0){
cout << i/10 << " ";
}
if(arr[i]!=0&&arr[i+1]==0)
cout << i/10 << endl;
}
}