#luogu整理 种树

洛谷 loj

(贪心)思路

同样,我们需要按照区间结束的位置排序,这样在结尾处放置可以使重合的区域变多。最后直接在区间末尾增加就好。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,ans;
int tmp = 0;
struct QJ_Happy{
	int l;int r;int t;
}a[40000];
int road[40000];
bool cmp(QJ_Happy a,QJ_Happy b){
	return a.l > b.l;
} 
int main(){
	cin >> n >> n;
	for(int i = 1;i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].t;
	sort(a+1,a+1+n,cmp);
	for(int i = 1;i <= n; i++){
		if(!a[i].t) continue;
		int k = 0;
		int tot = 0;
		for(int j = a[i].l;j <= a[i].r; j++) if(road[j]) k++;
		if(k < a[i].t)
			for(int j = a[i].l;j <= a[i].r; j++){
				if(!road[j] && k < a[i].t){
					k++;tot++;
					road[j]++;
				}
			}
		ans += tot;
	}
	cout << ans << endl;
	return 0;
}//lcez_cyc
原文地址:https://www.cnblogs.com/Cao-Yucong/p/12187510.html