C - Third-Party Software Gym - 101755C 国外区域赛 【贪心】

Pavel is developing a game. To do that, he needs functions available in a third-party library too famous to be called. It is known that the function i first appeared in version ai and existed until version bi, and starting from the version bi + 1, it is absent in this library.

The library is not free and Pavel needs all the functions. Which minimal number of versions he need to purchase to be able to use all the functions?

Input

The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of the functions.

Each of the next n lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ 109) — the interval of library versions where function i was available.

Output

In the first line output a single integer k — the minimal number of library versions need to be purchased to unlock all functions.

In the second line output k distinct integers — the numbers of versions need to be purchased.

If there are several possible answers, output any of them.

Example

Input

5
2 4
1 3
2 3
3 6
4 5

Output

2
3 4

一开始没看懂题意 问了一波师兄发现 是求区间覆盖问题 ,就是当前区间只要有一个数在下个区间下个区间就会被当前区间覆盖

师兄的意思貌似是尺取法  我感觉贪心能骚过 就贪心了

Q:我敲!为嘛贪心能过,请tm严格证明下!

A:这个做法的最终结果是把整条区间分为独立的若干小段,既然题意的意思是:必然有结果,所以 每次给的整条区间都可以分为独立的,不相交的若干小段

先按左端点排序 左端点相同就按右端点排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
struct node
{
	ll l,r;
}arr[maxn];

ll ans[maxn];

bool cmp(node a,node b)
{
	if(a.r!=b.r) return a.r<b.r;
	else return a.l<b.l;
}

int main()
{
	ll n;
	scanf("%d",&n);
	for(ll i=0;i<n;i++)
	{
		scanf("%lld%lld",&arr[i].l,&arr[i].r);
	}
	sort(arr,arr+n,cmp);
	ll cnt=1;
	ans[cnt]=arr[0].r;
	for(ll i=0;i<n;i++)
	{
		if(ans[cnt]>=arr[i].l)//ans能覆盖上 arr[i].l 
		{
			continue;
		}
		else
		{
			cnt++;
			ans[cnt]=arr[i].r;//ans无法覆盖,则取该arr的值 
		}
	}
	printf("%lld\n",cnt);
	for(ll i=1;i<cnt;i++)
	{
		printf("%lld ",ans[i]);
	}
	printf("%lld\n",ans[cnt]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值