题目描述:
某条街被划为n条路段,这n条路段依次编号为1到n 。每个路段最多可以种一棵树。现在居民们给出了h组建议,每组建议包含三个整数b,e,t ,表示居民希望在路段b到e之间至少要种t棵树。这些建议所给路段的区间可以交叉。请问:如果要满足所有居民的建议,至少要种多少棵树。
输入格式:
第一行为n,表示路段数。
第二行为h,表示建议数。
下面h行描述一条建议:b,e,t,用一个空格分隔。
输出格式:
输出只有一个数,为满足所有居民的建议,所需要种树的最少数量。
样例输入:
9
4
1 4 2
4 6 2
8 9 2
3 5 2
样例输出:
5
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 30005
#define MAXH 5005
int n, h, ans;
bool vis[MAXN];
struct node {int b, e, t;} a[MAXH];
bool cmp(node a, node b) {return a.e < b.e;}
int main(){
cin >> n >> h;
for (int i = 1; i <= h; i++) cin >> a[i].b >> a[i].e >> a[i].t;
sort(a + 1, a + 1 + h, cmp);
for (int i = 1; i <= h; i++) {
int tot = 0;
for (int j = a[i].b; j <= a[i].e; j++) tot += vis[j];
for (int j = a[i].e; j >= a[i].b; j--) {
if (tot >= a[i].t) break;
if (!vis[j]) vis[j] = 1, ans++, tot++;
}
}
cout << ans << endl;
return 0;
}