0_1 knapsack problem of deformation, this is the first error solution: DP when the volume of each item is set to 1, resulting in missing some results.
#include <iostream>#include<algorithm>#include<cstdio>#include<iomanip>#include<cmath>#include<vector>#include<string>using namespaceStd;typedefLong LongLL;#defineM 1000000007inta[ the];intdp[ the][ the];intMain () {intt,sum=0; scanf ("%d",&t); for(intI=0; i<t;i++) {scanf ("%d",&A[i]); Sum+=A[i]; } intM=m,aim,cnt=1, temp=sum; while(temp%Ten==0) {CNT*=Ten; Temp/=Ten; } Aim= temp/2*CNT; Sort (A,a+t); for(intI=0; i<t;i++) { if(i==0) dp[0][1] =A[i]; Else{dp[i][0] = dp[i-1][0]; for(intj=1; j<=t;j++) {Dp[i][j]= Max (dp[i-1][j],dp[i-1][j-1]+A[i]); if(dp[i][j]-aim>=0) M= Min (m,dp[i][j]-aim); } } } //sum-aim-m intM1 = max (sum-aim-m,aim+m), M2 = min (sum-aim-m,aim+m); printf ("%d", m1-M2); return 0;}
#include <cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;Const intn= the;intn,a[n],sum,su,dp[n* -];intMain () {scanf ("%d",&n); for(intI=1; i<=n;i++) scanf ("%d", &a[i]), sum+=A[i]; Su=sum/2; for(intI=1; i<=n;i++){ for(intj=su;j>=a[i];j--) Dp[j]=max (dp[j],dp[j-a[i]]+a[i]);} printf ("%d\n", sum-2*Dp[su]);return 0;}
1007 Positive integer Groupings