种树问题,某条街被划为n条路段,这n条路段依次编号为1...n。每个路段最多可以种一颗树。现在居民们给出了h组建议,每组建议包括三个整数b,e,t表示居民希望在路段b到e之间至少要种t棵树。这些建议所给路段的区间可以交叉。请问要满足所有居民的建议,至少要种多少棵树(贪心算法)
#define _CRT_SECURE_NO_WARNINGS 1 // scanf函数不安全
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int b, e, t;
} Proposal;
int cmp(const void* a, const void* b) {
Proposal* p1 = (Proposal*)a;
Proposal* p2 = (Proposal*)b;
return p2->t - p1->t;
}
int main() {
int n, h;
scanf("%d %d", &n, &h);
Proposal* proposals = (Proposal*)malloc(h * sizeof(Proposal));
for (int i = 0; i < h; i++) {
scanf("%d %d %d", &proposals[i].b, &proposals[i].e, &proposals[i].t);
}
qsort(proposals, h, sizeof(Proposal), cmp);
int* trees = (int*)calloc(n + 1, sizeof(int));
int ans = 0;
for (int i = 0;