LOJ#10001 种树(贪心)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:13   1317   0

题解:刚开始想用优先队列做……就是每一条建议取重叠最多的几个点种树,然后SE了= =数据比较大 = =

这题有个比较迷的地方……就是如果用while(cin>>n>>h)输入会WA……打开测试数据看了下发现有几组数据这样输入的话会有多组输出……= =另外就是点的数量是n,范围在30000,建议的范围在5000.弄混了好多次- -

按照右端点从小到大排序,每次都尽量往右种树。

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

struct node
{
    int b, e;
    int t;
    friend bool operator < (node a, node b)
    {
        return a.e<b.e;
    }
}se[5005];

bool vis[30005];

int main()
{
    int n, h;
    int ans;
    cin>>n>>h;
    memset(vis, 0, sizeof(vis));
    for(int i=1; i<=h; i++)
    {
        cin>>se[i].b>>se[i].e>>se[i].t;
    }
    sort(se+1, se+h+1);
    ans=0;
    for(int i=1; i<=h; i++)
    {
        int cnt=0;
        for(int j=se[i].b; j<=se[i].e; j++)
        {
            cnt+=vis[j];
        }
        for(int j=se[i].e; j>=se[i].b && cnt<se[i].t; j--)
        {
            if(!vis[j])
            {
                vis[j]=1;
                cnt++;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP