首页 > 其他分享 >#10001. 「一本通 1.1 例 2」种树

#10001. 「一本通 1.1 例 2」种树

时间:2022-08-16 20:24:33浏览次数:54  
标签:node 10001 int tot ++ num 种树 ans 1.1

尽量在交错处种树,所以按结尾的大小排序,然后从后往前依次种 \(x\) 棵树( \(x\) 为该区域应种的树减去已在该区域应种的树)。

#include<bits/stdc++.h>
using namespace std;
namespace Solution
{
	int n;
	bool v[300001];
	struct node
	{
		int l,r,num;
	}a[50001];
	int h;
	bool cmp(node a,node b)
	{
		return a.r<b.r;
	}
	void read()
	{
		cin>>n>>h;
		for(int i=1; i<=h; i++)
		{
			cin>>a[i].l>>a[i].r>>a[i].num;
		}
	}
	int answer()
	{
		sort(a+1,a+h+1,cmp);
		int ans=0;
		for(register int i=1; i<=h; i++)
		{
			int tot=0;
			for(register int j=a[i].r; j>=a[i].l; j--)
			{
				if(v[j])tot++;
			}
			if(tot>=a[i].num)continue;
			for(register int j=a[i].r; j>=a[i].l; j--)
			{
				if(v[j]==0)
				{
					v[j]=1;
					tot++;ans++;
				}
				if(tot>=a[i].num)break;
			}
		}
		ans=0;
		for(int i=1; i<=n; i++)ans+=v[i];
		return ans;
	}
}
signed main()
{
	Solution::read();
	cout<<Solution::answer()<<endl;
}

标签:node,10001,int,tot,++,num,种树,ans,1.1
From: https://www.cnblogs.com/dadidididi/p/16592832.html

相关文章