整数区间(贪心)

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int l;
    int r;
}a[10010];
bool cmp(node x,node y)
{
    if(x.l==y.l)
    {
        return x.r>y.r;
    }
    else
    {
        return x.l>y.l;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].l>>a[i].r;    
    }    
    sort(a+1,a+n+1,cmp);
    int tl,tr;
    tl=a[1].l;
    tr=a[1].l+1;
    int ans=2;
    for(int i=2;i<=n;i++)
    {
        if(a[i].r<tl)
        {
            ans+=2;
            tl=a[i].l;
            tr=a[i].l+1;
        }
        else if(a[i].r<tr)
        {
            ans+=1;
            tr=tl;
            tl=a[i].l;
        }
    }
    cout<<ans;
    return 0;
}
/*
4
3 6 
2 4 
0 2 
4 7
/*
原文地址:https://www.cnblogs.com/Chri-K/p/13819619.html