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
题意
给你n个区间 问最少选几个数 使这些数存在所以区间
思路
贪心 对区间排序后 选取第一个数存在最左边区间的最大值,如果之后区间不符合 则选取不符合区间最大值
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
struct node
{
int l,r;
}a[200100];
bool cmp(node a,node b)
{
if(a.r<b.r) return 1;
else if(a.r==b.r&&a.l<b.l) return 1;
else return 0;
}
int main()
{
int n,i,pre=-1;
queue<int> q;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(pre<a[i].l)
{
pre = a[i].r;
q.push(pre);
}
}
printf("%d\n",q.size());
while(!q.empty())
{
printf("%d",q.front());
q.pop();
if(!q.empty())
{
printf(" ");
}
}
return 0;
}