Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19403 Accepted Submission(s): 6785
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<N<1000) kinds of facilities (different value, different kinds).
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<N<1000) kinds of facilities (different value, different kinds).
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 (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
A 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
01背包问题,为什么要说是多重背包呢,因为它本来是一道多重背包的题,只不过是自己没做过
所以就转化后按01背包做了,01背包固定的模式固定的代码两个循环没什么可说的,但要理解每
道题都有转化,这道题也一样,另附有lala的多重背包的代码一份,以供学习
minlala多重背包
所以就转化后按01背包做了,01背包固定的模式固定的代码两个循环没什么可说的,但要理解每
道题都有转化,这道题也一样,另附有lala的多重背包的代码一份,以供学习
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int N,w[50005],f[500005]; int main() { int i,j,p,sum,sum1,temp,k; while(scanf("%d",&N)&&N>=0) { memset(w,0,sizeof(w)); memset(f,0,sizeof(f)); sum=0; for(i=1,temp=0;temp<N;temp++,i++) { scanf("%d%d",&w[i],&p); sum=sum+w[i]; if(p>1) { for(j=1;j<p;j++,i++) { w[i+1]=w[i]; sum+=w[i]; } } } temp=i; sum1=sum; sum=sum/2; for(i=1;i<=temp;i++) for(j=sum;j>=0;j--) { if(j>=w[i]) { if(f[j]<f[j-w[i]]+w[i]) f[j]=f[j-w[i]]+w[i]; else f[j]=f[j]; } else f[j]=f[j]; } printf("%d %d\n",sum1-f[sum],f[sum]); }
/*多维背包 98ms*/ #include<stdio.h> #include<math.h> #include<string.h> int v[59]; int w[59]; int dp[155010]; int N,value; int max(int a,int b) { return a>b?a:b; } void wan(int vv)//完全背包 { for(int i=vv;i<=value;i++) dp[i] = max(dp[i],dp[i-vv]+vv); } void zone(int vv)//01背包 { for(int i=value;i>=vv;i--) dp[i] = max(dp[i],dp[i-vv]+vv); } int main() { int i,j; while(scanf("%d",&N)&&N>=0) { if(N==0){printf("0 0\n");continue;} value = 0; j = 1; for(i=1;i<=N;i++) { scanf("%d%d",&v[i],&w[i]); value+=v[i]*w[i]; } int aaaa = value; value = value/2; memset(dp,0,sizeof(dp)); for(i=1;i<=N;i++) { if(v[i]*w[i]>=value) { wan(v[i]); continue; } int BB = 1,B = w[i]; while(BB<B) { zone(v[i]*BB); B-=BB; BB = BB*2; } zone(B*v[i]); } printf("%d %d\n",aaaa-dp[value],dp[value]); } return 0; }
return 0;}
minlala多重背包