洛谷 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

相关文章:

  • 2022-01-24
  • 2021-09-29
  • 2021-05-16
  • 2021-05-25
  • 2021-09-23
  • 2021-10-11
  • 2022-01-28
  • 2021-06-16
猜你喜欢
  • 2022-02-20
  • 2022-02-18
  • 2021-12-04
  • 2021-07-17
  • 2022-01-30
  • 2022-02-26
  • 2021-12-30
相关资源
相似解决方案