Selfish Grazing

Selfish Grazing

https://ac.nowcoder.com/acm/problem/24867

典型的贪心题目

1.首先我们想能在规定时间内尽可能的安排更多的活动,那么我们就要按照活动结束的时间来排序,先结束的排在前面,这样我们就能举办更多的活动
2.之后我们依次对他遍历,只要满足要求便+1,并且同时更新last的值,为后续比较做准备

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
struct node
{
    int sta, en;
    bool operator<(const node x) const
    {
        return en < x.en;
    }
} w[maxn];
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        int ans = 0;
        for (int i = 1; i <= n; ++i)
            scanf("%d%d", &w[i].sta, &w[i].en);
        sort(w + 1, w + n + 1);
        int last = -1;
        for (int i = 1; i <= n; ++i)
            if (w[i].sta >= last)
            {
                ans++;
                last = w[i].en;
            }
        printf("%d\n", ans);
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
08-21 13:43
被挂麻了已经
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务