luogu P4823 [TJOI2013]拯救小矮人

题解 传送门

对于每个小矮人,一旦他出去浪,就再也回不来了
所以肯定要让不容易出去的,先出去浪
关爱老幼病残孕及带小孩的乘客
所以我们按小矮人的身高+手长排遍序
因为长得高的贡献可能更大
而手长的更容易出去
.
.
.
排完序后
直接跑遍背包,求出走掉i个小矮人所剩余的最大高度

代码

#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
#define db double
#define in inline
#define get getchar()
in int read()
{
	int t=0,x=1; char ch=get;
	while ((ch<'0' || ch>'9') && ch!='-') ch=get;
	if(ch=='-') ch=get,x=-1;
	while (ch<='9' && ch>='0') t=t*10+ch-'0', ch=get;
	return t*x;
}
const int _=2010;
struct human{
	int a,b,s;
}p[_]; //小矮人(为啥是human???)
int n,h,f[_];

in int cmp(human a,human b){return a.s<b.s;}

int main()
{
	n=read();
	memset(f,-1,sizeof(f));f[0]=0;
	for(re int i=1;i<=n;i++)
	{
		p[i].a=read(),p[i].b=read();
		p[i].s=p[i].a+p[i].b,f[0]+=p[i].a;
	}
	h=read();
	sort(p+1,p+n+1,cmp);
	int ans=0;
	for(re int i=1;i<=n;i++)
	{
		for(re int j=ans;j>=0;j--)
		{
			if(p[i].b+f[j]>=h)f[j+1]=max(f[j+1],f[j]-p[i].a);
			if(f[ans+1]>=0)ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

posted @ 2019-04-04 17:04  yzhx  阅读(131)  评论(0编辑  收藏  举报