标签:style blog http color strong 数据
2 5 2 6 5 8 9 3 2 1 5
0 2
解题思路:将所有的价值求和折半,再进行01背包求解。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 using namespace std; 11 int w[1001]; 12 int dp[100010]; 13 int main(){ 14 int kase,n,i,j,sum,avg; 15 scanf("%d",&kase); 16 while(kase--){ 17 scanf("%d",&n); 18 sum = 0; 19 for(i = 1; i <= n; i++){ 20 scanf("%d",w+i); 21 sum += w[i]; 22 } 23 avg = sum/2; 24 memset(dp,0,sizeof(dp)); 25 for(i = 1; i <= n; i++){ 26 for(j = avg; j >= w[i]; j--) 27 dp[j] = max(dp[j],dp[j-w[i]]+w[i]); 28 } 29 printf("%d\n",abs(2*dp[avg]-sum)); 30 } 31 return 0; 32 }
NYOJ 456 邮票分你一半,布布扣,bubuko.com
标签:style blog http color strong 数据
原文地址:http://www.cnblogs.com/crackpotisback/p/3833798.html