种树

题目描述

某条街被划为 nn 条路段,这 nn 条路段依次编号为 1\dots n1…n。每个路段最多可以种一棵树。现在居民们给出了 hh 组建议,每组建议包含三个整数 b,e,tb,e,t,表示居民希望在路段 bb 到 ee之间至少要种 tt 棵树。这些建议所给路段的区间可以交叉。请问:如果要满足所有居民的建议,至少要种多少棵树。

输入格式

第一行为 nn,表示路段数。

第二行为 hh,表示建议数。

下面 hh 行描述一条建议:b, e, tb,e,t,用一个空格分隔。

输出格式

输出只有一个数,为满足所有居民的建议,所需要种树的最少数量。

样例

样例输入

9
4
1 4 2
4 6 2
8 9 2
3 5 2

样例输出

5
#include <iostream>
#include <cstdio>
#include <set>

using namespace std;
const int MAX = 50005;
struct node{
    int x,y,s;

    friend bool operator <(const node& n1,const node& n2){
        if(n1.y == n2.y)return n1.x < n2.x;//when y1 = y2 then compare x1 and x2
        else return n1.y < n2.y;//when y1 not equl y2 then compare y1 and y2
    }
};

int main(int argc, char const *argv[]) {
    int n,h;

    scanf("%d%d",&n,&h);
    int q[MAX] = {0};

    set<node> st;//sort with y
    for(int i = 0;i < h;i++){
        node nd;
        scanf("%d%d%d",&nd.x,&nd.y,&nd.s);

        st.insert(nd);
    }
    //record the number of trees
    int sum = 0;
    for(auto k:st){
        int tem1 = 0,tem2 = 0;
        for(int i = k.x;i <= k.y;i++)
            if(q[i] == 1)tem1++;//these points can be borrow

        tem2 = k.s - tem1;//The number of points that need to be increased

        for(int i = k.y;i > 0;i--){
            if(tem2 <= 0)break;//key issue  -> '<='
            if(q[i] != 1){
                q[i] = 1;
                sum++;//add the number of tree
                tem2--;
            }
        }
    }

    printf("%d\n",sum);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值