• 贪心之活动选择问题


    将活动按照结束时间单调递增排序

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    struct node
    {
        int b,e;
    }a[1000],temp;
    
    int cmp(node x,node y)
    {
        return x.e<y.e;
    }
    
    int main()
    {
        int i,j,n,p,q,x;
        while(~scanf("%d",&n))
        {
            int count=0;
            for(i=0;i<n;i++)
            scanf("%d%d",&a[i].b,&a[i].e);
            sort(a,a+n,cmp);
    
            x=a[0].e;
            for(j=1;j<n;j++)
            {
                if(a[j].b>=x)  //如果开始时间等于前一个结束时间,就可以举办
                {
                    count++;
                    x=a[j].e;
                }
            }
    
            printf("%d
    ",count+1);  //+1是因为,x是从第一个结束时间开始的,那么他肯定举办了第一次
        }
        return 0;
    }
    
    /**
    5
    1 3
    2 5
    4 7
    6 9
    8 10
    **/
  • 相关阅读:
    Python GIL-------全局解释器锁
    JavaScript简介
    MongoDB查询
    创建、更新和删除文档
    MongoDB基础知识
    Linux安装mysql
    函数、变量、参数
    循环语句
    控制语句
    集合
  • 原文地址:https://www.cnblogs.com/Fy1999/p/9030237.html
Copyright © 2020-2023  润新知