标签:
题目链接:
http://acm.nyist.net/JudgeOnline/problem.php?pid=325
http://acm.nyist.net/JudgeOnline/problem.php?pid=456
太久没有接触DP了,分类把这个题目分到了搜索,其实是01背包,有意思的是这里的价值也是重量,我最多装sum/2,那么每个邮票的价值也就变成了重量,并且要尽可能的装满,价值也是价值的含义
两道题几乎一样。
#include <stdio.h> #include <math.h> #include <string.h> int val[10050]; int dp [200050]; int main() { int n; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); memset(val,0,sizeof(val)); int sum = 0; for(int i=1; i<=n; i++) { scanf("%d",&val[i]); sum+=val[i]; } for(int i=1; i<=n; i++) { for(int j=sum/2; j>=val[i]; j--) dp[j]=(dp[j]>dp[j-val[i]]+val[i]?dp[j]:dp[j-val[i]]+val[i]); } int tmp1 = dp[sum/2]; int tmp2 = sum - tmp1; int ans = (int)fabs(tmp1-tmp2); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TreeDream/p/5720492.html