Topic Link: Click to open the link
Test instructions: n coins, given the value of each coin, require that these coins be divided into two groups, making the value of the two groups as small as possible.
It can be found that if you can split equally, then the value difference must be 0, then from SUM/2 to 0 enumeration I, if the value combination of the above coin can be composed of I then sum-i-i is the answer. How to determine whether for a number I with these coins can be gathered out, with a backpack sentence can be, note is 01 backpack.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 55005#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1<<40+10#define pp pair<int,int> #define ull unsigned long longusin G namespace Std;int v[115],dp[maxn],n,s;void solve () {memset (dp,0,sizeof (DP));DP [0]=1;for (int i=1;i<=n;i++) for ( int j=s/2;j>=0;j--) dp[j+v[i]]+=dp[j];for (int i=s/2;i>=0;i--) if (Dp[i]) {printf ("%d\n", s-i*2); return;}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), s=0;for (int i=1;i<=n;i++) {scanf ("%d", v+i); s+ =v[i];} Solve ();} return 0;}
Uva 562-dividing coins (DP)