[贪心算法]「一本通 1.1 例 2」种树

P1250 种树luogu

[一本通 1.1 例 2」种树

简洁题意:

给定h(<=5000)个可覆盖区间 (区间范围 3 × 1 0 4 3 \times10^4 3×104) ,每个单位可种一树,每个区间需种够 a i a_i ai 棵树,求最少树数

题解 :

贪心,按右端点升序排序,尽量把树在范围内从后往前种,这样就更可能被包含在后面的区间里。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{int s,e,t;}a[30100];
bool cmp(node x, node y) { return  x.e<y.e; }
bool tr[30100];
int main()
{
	int n,h,ans=0;
	scanf("%d %d",&n,&h);
	for(int i=1;i<=h;i++) scanf("%d %d %d",&a[i].s,&a[i].e,&a[i].t);
	sort(a+1,a+1+h,cmp);
	for(int i=1;i<=h;i++)
	{
		int count=0;
		for(int j=a[i].s;j<=a[i].e;j++)
			if(tr[j]) count++;//统计已种树 
		if(count>=a[i].t) continue;
		for(int j=a[i].e;j>=a[i].s;j--)
		{//不够从后往前补 
			if(!tr[j]) tr[j]=true,count++,ans++;
			if(count==a[i].t) break;
		}		
	}
	printf("%d",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值