#10000. 「一本通 1.1 例 1」活动安排(贪心)

传送门

题意:

在这里插入图片描述

4
1 3
4 6
2 5
1 7

2

1<=n<=1000

思路:

为了能有更多的活动,对于每一个活动越早结束,其他活动越可能发生。
按照活动结束时间排序即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
typedef long long ll;
#define PII make_pair
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=1e5+50;
const int inf=0x3f3f3f3f;
const int M=5000*4;
struct sa{
    int l,r;
}p[1050];
int cmp(sa a,sa b){
    if(a.r!=b.r)return a.r<b.r;
    else return a.l<b.l;
}
int main()
{   
    int n;
    scanf("%d",&n);
    rep(i,1,n){
        scanf("%d%d",&p[i].l,&p[i].r);
    }
    sort(p+1,p+n+1,cmp);
    int ans=1;
    int time=p[1].r;//活动结束的时间
    rep(i,2,n){
        if(p[i].l>=time){
            ans++;
            time=p[i].r;
        }
    }
    printf("%d\n",ans);
    return 0;
} 

发布了142 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104592677