uva 562 - Dividing coins
题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503
思路:裸的01背包
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> using namespace std; #define maxn 50010 int dp[maxn]; int coin[110]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int total=0; for(int i=1;i<=n;i++) { scanf("%d",&coin[i]); total+=coin[i]; } int half=total/2; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=half;j>=coin[i];j--) dp[j]=max(dp[j],dp[j-coin[i]]+coin[i]); half=dp[half]; printf("%d\n",abs(2*half-total)); } return 0; }