Uvs -- 562 Dividing coins + dp, dividing
Question:
Give a bunch of coins and divide them into two parts to minimize the difference between the two parts; output the smallest difference.
Ideas:
After thinking for a long time, I did not expect a suitable state transition equation. After reading other people's question solutions, you can turn it into a backpack.
We use all the coins and half of them as the backpack capacity, and then regard the coin price as its own nominal value. The maximum capacity that can be installed in the backpack.
That is, the number of coins that a person receives.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; int main () {int I, j, k, t; scanf ("% d ", & t); while (t --) {int m, a [110], dp [100000], sum = 0; memset (dp, 0, sizeof (dp )); scanf ("% d", & m); for (I = 1; I <= m; I ++) {scanf ("% d ", & a [I]); sum + = a [I];} int V = sum/2; for (I = 1; I <= m; I ++) for (j = V; j> = a [I]; j --) dp [j] = max (dp [j], dp [j-a [I] + a [I]); printf ("% d \ n", sum-2 * dp [V]);} return 0 ;}