「1.1」种树
问题背景
「一本通1.1 例2」
题目描述
某条街被划为 n 条路段,这 n 条路段依次编号为 1…n。每个路段最多可以种一棵树。现在居民们给出了 h 组建议,每组建议包含三个整数 b,e,t,表示居民希望在路段 b 到 e 之间至少要种 t 棵树。这些建议所给路段的区间可以交叉。请问:如果要满足所有居民的建议,至少要种多少棵树。
输入格式
第一行为 n,表示路段数。
第二行为 h,表示建议数。
下面 h 行描述一条建议:b,e,t,用一个空格分隔。
输出格式
输出只有一个数,为满足所有居民的建议,所需要种树的最少数量。
样例输入1
9
4
1 4 2
4 6 2
8 9 2
3 5 2
样例输出1
5
注释说明
30% 的数据满足 0<n≤1000,0<h≤500;
100% 的数据满足 0<n≤3×10^4 ,h≤5000,0<b≤e≤3×10^4 ,t≤e-b+1。
#include<bits/stdc++.h>
using namespace std;
int n,h,ans,x,z[30005];
struct ed{
int b,e,t;
}a[5005];
bool cmp(ed x,ed y){
if(x.e==y.e)return x.b>y.b;
return x.e<y.e;
}
int main(){
cin>>n>>h;
for(int i=1;i<=h;i++)scanf("%d%d%d",&a[i].b,&a[i].e,&a[i].t);
sort(1+a,a+1+h,cmp);
// for(int i=1;i<=h;i++)printf("%d %d %d\n",a[i].b,a[i].e,a[i].t);//puts(" ");
for(int k=1;k<=h;k++){
int al=0;
for(int i=a[k].b;i<=a[k].e;i++){
if(z[i])al++;
//
}//printf("%d\n",al);
if(al>=a[k].t)continue;
for(int i=a[k].e;i>=a[k].b;i--){
if(al>=a[k].t)break;
if(z[i]==0){
z[i]=1;
ans++;
al++;
}
}
}
cout<<ans;
}