hdu-1171-Big Event in HDU-一维背包转换为01背包或多重背包(或用母函数解决)

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47907    Accepted Submission(s): 16464


Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0A test case starting with a negative integer terminates input and this test case is not to be processed.
 

Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
 

Sample Input
 
   
2
10 1
20 1
3
10 1 
20 2
30 1
-1
 

Sample Output
 
   
20 10
40 40
 

Author
lcy
 

Recommend

We have carefully selected several similar problems for you:  2159 2844 1087 1176 1114 

01背包解决

#include
#include
#include
using namespace std;
int v[5005];
int dp[255555];

int main()
{
	int n,i,j,l,a,b,sum;
	while(~scanf("%d",&n)&&n>0)
	{
		memset(dp,0,sizeof(dp));
		memset(v,0,sizeof(v)); 
		l=0;
		sum=0;
		for(i=0;i=v[i];j--)
			dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
		
		printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
	}
    
	return 0;
} 

多重背包

#include
#include
#include
using namespace std;
int v[55];
int dp[250015];
int num[55];
int main()
{
	int n,i,j,k,l,a,b,sum;
	while(~scanf("%d",&n)&&n>0)
	{
		memset(dp,0,sizeof(dp));
		sum=0;
		for(i=0;i=v[i];j--)
			  dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
		
		printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
	} 
	return 0;
} 

母函数模板正在研究附上

母函数模板

#include
typedef long long LL;
const int N = 100 + 5;//假如题目只问到100为止 
const int MAX = 3;//题目只有1,2,3这3种邮票 
LL c1[N], c2[N];//c2是临时合并的多项式,c1是最终合并的多项式 
int n;
void init(){
    c1[0] = 1;//一开始0的情况算一种 
    for(int i = 1; i <= MAX; i ++){//把1分到MAXN的邮票合并,变成一个多项式 
        for(int j = 0; j < N; j += i){//i分的邮票,步长是i
            for(int k = 0; j + k < N; k ++){//从x^0到x^N遍历一遍 
                c2[j + k] += c1[k];//因为j的所有项系数为1,所以c1[k]可以看成c1[k]*1; 
            }
        } 
        for(int j = 0; j < N; j ++){//把c2的数据抄到c1,清空c2 
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
} 
int main(){
    init();
    while(scanf("%d", &n) != EOF){
        printf("%I64d\n", c1[n]);
    }
}

大佬代码

#include
int c1[250001],c2[250001];    //这个地方开始把题目看错了,导致数组开小了
int a[55][2];
int main()
{
 int i,j,k,s,n;
 while(scanf("%d",&n)&&n>0)
 {
  s=0;     //开始忘了初始化,输入数据后,一直不能输出
  for(i=0;i


你可能感兴趣的:(----acm----动态规划)