NYOJ_456_邮票分你一半
很水的01背包,注意用条件表达式优化时间就好了
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> using namespace std; int t,n,dp[50003],v,mid_v,w[50003],i,j; int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0,v=0;i<n;++i) { scanf("%d",&w[i]); v+=w[i]; } mid_v=v>>1; for(i=0;i<=mid_v;++i) dp[i]=0; for(i=0;i<n;++i) for(j=mid_v;j>=w[i];--j) { dp[j]=dp[j]<dp[j-w[i]]+w[i]?dp[j-w[i]]+w[i]:dp[j]; } printf("%d\n",v-dp[mid_v]*2); } return 0; }