Give G kinds of gems, B bags, and S, S represents the time when each color of the gem together S can become a magic stone
There are N kinds of gems in each bag, which are c1, c2 .......
Then the two take the bag in turn, each of which can only be taken once, and take out the bag to put the gem on the ground.
If it turns into a magic stone, it will take away the magic stone. The next time this person will take the bag, it will change if it does not become a magic stone.
The Magic Stone number is to get the score and ask the best time difference between the two.
Shape-pressure memory search
A total of 21 packages are saved in the status of the current package.
No matter how it is retrieved, the number of magic stones finally obtained is certain.
Dp [I] indicates the highest score that can be obtained by the first hand in the I state.
# Include "stdio. h "# include" string. h "int aim, g, B, s; int dp [1 <22], c [25] [10]; int bit [25]; int inf = 0x3f3f3f; int Max (int a, int B) {if (a <B) return B; else return a;} int dfs (int cur, int sum, int temp []) {int ans, I, next, j, w, cnt; int mark [10]; if (cur = aim | sum = 0) return 0; if (dp [cur]! = Inf) return dp [cur]; ans = 0; for (I = 1; I <= B; I ++) if (cur & bit [I]) {next = cur ^ bit [I]; cnt = 0; for (j = 1; j <= g; j ++) {mark [j] = temp [j] + c [I] [j]; cnt + = mark [j]/s; mark [j] % = s ;} if (cnt> 0) w = cnt + dfs (next, sum-cnt, mark); else w = sum-dfs (next, sum, mark ); ans = Max (ans, w);} return dp [cur] = ans;} int main () {int I, n, x, sum, aim, ans; int all [10], mark [25]; bit [1] = 1; for (I = 2; I <= 22; I ++) bit [I] = bit [i-1] * 2; while (scanf ("% d % D ", & g, & B, & s )! = EOF) {if (g + B + s = 0) break; memset (c, 0, sizeof (c); memset (all, 0, sizeof (all); for (I = 1; I <= B; I ++) {scanf ("% d", & n); while (n --) {scanf ("% d", & x); c [I] [x] ++; all [x] ++ ;}} sum = 0; for (I = 1; I <= g; I ++) sum + = all [I]/s; aim = bit [B + 1]-1; memset (mark, 0, sizeof (mark); memset (dp, inf, sizeof (dp); ans = dfs (0, 0, mark); printf ("% d \ n ", ans-(sum-ans);} return 0 ;}
HDU 4778 memory-based search & Compression