贪心

来源:互联网 发布:全国小区数据库 编辑:程序博客网 时间:2024/04/27 11:14
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct time
{
    int x;
    int y;
} a[25001];
int cmp(time a,time b)
{
    return a.x<b.x;
}
int main()
{
    int i,n,T;
    scanf("%d%d",&n,&T);
    for (i=1; i<=n; ++i)
        scanf("%d%d",&a[i].x,&a[i].y);
    sort(a+1,a+n+1,cmp);
    a[n+1].x=0x7fffffff;
    int t=0,temp=0,ans=0;
    int f=0;
    for (i=1; i<=n; ++i)
        if (a[i].x<=t+1)
        {
            if(temp<a[i].y)
            {
                temp=a[i].y;
                f=1;
            }
            if(a[i+1].x>t+1 && f)
            {
                t=temp;
                ++ans;
                f=0;
            }
        }
    if (t<T)
        printf("-1\n");
    else
        printf("%d\n",ans);
    return 0;
}
0 0