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;
}