标签:dp
Description
Input
Output
Sample Input
2 10 1 20 1 3 10 1 20 2 30 1 -1
Sample Output
20 10 40 40题意:有n种物品,然后每一行的两个数,第一个是价值,第二个是商品的数量,把这n种物品分为A,B两份,要求A,B之间的差值大于等于0,且A>=B#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int dp[100010]; int main() { int p[1010],num[1010]; int n,i,j,k; int sum=0,ave; while(~scanf("%d",&n)) { if(n<=-1)//这里注意 break; sum=0; for(i=1;i<=n;i++) { scanf("%d %d",&p[i],&num[i]); sum+=p[i]*num[i]; } memset(dp,0,sizeof(dp)); ave=sum/2; for(i=1;i<=n;i++) for(j=1;j<=num[i];j++) for(k=ave;k>=p[i];k--) dp[k]=max(dp[k],dp[k-p[i]]+p[i]); printf("%d %d\n",sum-dp[sum/2],dp[sum/2]); } return 0; }
Big Event in HDU(背包九讲_多重背包转01背包)
标签:dp
原文地址:http://blog.csdn.net/u013486414/article/details/38900079