2019暑假集训 种树
第二行为 h,表示房子的数目。
下面 h 行描述居民的需要:b,e,t(0<b≤e≤30000,t≤e-b+1)分别用一个空格分开。
9
4
1 4 2
4 6 2
8 9 2
3 5 2
5
100% 的数据满足 0<n≤3×10^4,h≤5000,0<b≤e≤3×10^4 ,t≤e−b+1。
明显的贪心 有兴趣的同学可以参见类似题目 会场安排
#include<iostream> #include<algorithm> using namespace std; int n,h,book[30050],ans; struct node { int b,e,t; bool operator<(const node& o) const { return e<o.e; } }a[5005];//区间 int main() { scanf("%d%d",&n,&h); for(int i=1;i<=h;i++)scanf("%d%d%d",&a[i].b,&a[i].e,&a[i].t); sort(a+1,a+h+1);//按照左边界排序 for(int i=1;i<=h;i++) { int cnt=0; for(int j=a[i].b;j<=a[i].e;j++) if(book[j])cnt++;//统计上个区间种在右边界上的树是否足够本区间要去 if(cnt>=a[i].t)continue; for(int j=a[i].e;j>=a[i].b;j--) { if(book[j]==0) { book[j]=1; cnt++; if(cnt==a[i].t)break; }//不够的补在右区间 } } for(int i=1;i<=n;i++) if(book[i])ans++; printf("%d",ans); return 0; }