Segments CodeForces - 22D

You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?

Input

The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.

Output

The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.

Examples

Input

2
0 2
2 5

Output

1
2 

Input

5
0 3
4 2
4 8
8 10
7 7

Output

3
7 10 3

题解:区间贪心,按左端点升序对线段排序

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+3;
struct node{
	int l,r;	
}a[N];
bool cmp(node x,node y){
	if(x.l==y.l) return x.r<y.r;
	return x.l<y.l;
}
int book[N];
int main(){
	int n; cin>>n;
	for(int i=0;i<n;i++){
		int x,y; cin>>x>>y;
		if(x>y) a[i].l=y,a[i].r=x;
		else a[i].l=x,a[i].r=y;
	}
	sort(a,a+n,cmp);
	vector<int> c;
	int r=a[0].r;
        //按for循环也可以过,若用while循环要特判一下n==1的情况,因为这个卡了,我好菜。。。
//	for(int i=1;i<n;i++){
//		if(a[i].l<=r) r=min(r,a[i].r);
//		else{
//			c.push_back(r);
//			r=a[i].r;
//		}
//	}		c.push_back(r);
	int i=1;
	if(n==1) c.push_back(r);
	else {
		while(i<n){
			while(i<n&&a[i].l<=r){
				if(a[i].r<r) r=a[i].r;
				i++;
			}
			c.push_back(r);
			r=a[i].r;
		}
	}
	cout<<c.size()<<endl;
	int num=c.size();
	for(int i=0;i<num;i++){
		cout<<c[i]<<' ';
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值