[HDU-1171] 生成函数入门题

依旧是最基础的生成函数。

(1+x^i)*……

直接用lst每次上界最大值优化就够了。(因为最坏情况是尽量跑满,所以没必要记录已有的数值)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back

const int M = 3e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int f[2][M];
int w[51];
int vs[M],nm[51];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	while(1)
	{
		int n;cin>>n;
		if(n<=-1)break;
		memset(f,0,sizeof(f));
		int sm=0;
		for(int i=1;i<=n;i++)cin>>w[i]>>nm[i],sm+=w[i]*nm[i];
	  	f[0][0]=1;
	  	int lst=0;
	  	for(int i=1;i<=n;i++)
	  	{
	  		for(int x=0;x<=lst;x++)
	  			for(int k=0;k<=nm[i];k++)
	  				f[i&1][x+k*w[i]]+=f[(i-1)&1][x];
			lst+=w[i]*nm[i];
		}
		for(int i=sm/2;i<=sm;i++)
		if(f[n&1][i])
		{
		//	cout<<f[n][i]<<"- -    "<<endl;
			int	a=i,b=sm-i;
			if(a<b)swap(a,b);
			cout<<a<<" "<<b<<endl;
			break;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bjfu170203101/article/details/107593100