Loading

元旦晚会

P1986 元旦晚会

一个很典型的贪心。考虑把右端点从大到小排序,尽量选择靠右的点,因为这样重叠的概率高。

#include<bits/stdc++.h>
using namespace std;
const int N=30005;
struct node{
	int l,r,k;
	bool operator < (const node &rhs)const{return rhs.r>r;}
}a[N];
int n,m,ans;
bool vis[N];
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;++i)
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].k);
	sort(a+1,a+m+1);
	for(int i=1;i<=m;++i)
	{
		int sum=0;
		for(int j=a[i].l;j<=a[i].r;++j)
			if(vis[j])++sum;
		if(sum>=a[i].k)continue;
		for(int j=a[i].r;j>=a[i].l&&sum<a[i].k;--j)
			if(!vis[j])vis[j]=true,++sum,++ans;
	}
	printf("%d\n",ans);
	return 0;
}

差分约束也可以做。设 \(f_i\) 表示前 \(i\) 个位置的人数总和。则有
\(\because\) 人不能是负数 \(\therefore f_{i}-f_{i-1}\ge 0\)
\(\because\) \(a_i \to b_i\) 至少\(c_i\) 个人\(\therefore f_{b_i}-f_{a_i-1}\ge c_i\)
\(\because\) 每人最多1个话筒 \(\therefore f_i+1\ge f_{i+1}\)

posted @ 2020-02-16 19:16  zzctommy  阅读(118)  评论(0编辑  收藏  举报