Question: There are some stones, each of which has a value (1 ~ An integer in column 6). Now we divide the stone into two groups,
Ask if they can be divided into two groups with the same value.
Analysis: DP, 01 backpack. If the total size of a backpack is half, you can determine whether it can be fully filled.
Note: If the data size is large, the binary shard Shard is used.
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; int f [60001], C [6], W [6] = {1, 2, 3, 4, 5, 6}; int P [60001]; int main () {int T = 1; while (scanf ("% d ", & C [0], & C [1], & C [2], & C [3], & C [4], & C [5]) {int sum = 0; For (INT I = 0; I <6; ++ I) sum + = C [I] * W [I]; if (sum = 0) break; // binary split int COUNT = 0; For (INT I = 0; I <6; ++ I) {int K = 1, cc = C [I]; while (k <CC) {P [count ++] = K * W [ I]; CC-= K; k <= 1;} If (CC) P [count ++] = Cc * W [I];} printf ("collection # % d: \ n", t ++); If (sum % 2) printf ("can't be divided. \ n "); else {int M = sum/2; memset (F, 0, sizeof (f); F [0] = 1; for (INT I = 0; I <count; ++ I) for (INT v = m; V> = P [I]; -- V) if (F [V-P [I]) f [v] = 1; if (! F [m]) printf ("can't be divided. \ n "); else printf (" can be divided. \ n ") ;}} return 0 ;}
Ultraviolet A 711-dividing up