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

猜你喜欢

转载自blog.csdn.net/weixin_41544329/article/details/84098934