Sicily merge



 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]

Input

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].

Output

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;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值