HDU 4778 gems fight! (DP)

Source: Internet
Author: User
HDU 4778 gems fight!

Question Link

Question: There are n backpacks and some gems in the bag. Now Alice and you choose a backpack in turn, drop the jewels in the bag into the pot, and if there are gems in the pot to S, you will get the magic stone, and you can continue to choose a backpack. Both of them will follow the best strategy and ask the last two members how much the magic stone will be.

Idea: DP, DP [s] indicates the value when the status of the selected backpack is s, and then you can perform a memory-based search. Note that if the current magic stone is generated, continue to add it; otherwise, you can subtract it.

Code:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int N = 21;int g, b, s, dp[(1<<N) + 5], vis[(1<<N) + 5];int yu[(1<<N) + 5][10];struct Page {    int num;    int a[10];} p[N];int cal(int S, int u) {    int ans = 0;    for (int i = 1; i <= g; i++) {ans += (yu[S][i] + p[u].a[i]) / s;    }    return ans;}int dfs(int now) {    int &tmp = dp[now];    if (vis[now]) return tmp;    vis[now] = 1;    tmp = 0;    int Min = INF, Max = -INF;    if (now == (1<<b) - 1) return tmp;    for (int i = 0; i < b; i++) {if (now&(1<<i)) continue;int sb = cal(now, i);int next = (now|(1<<i));if (sb != 0) {    Max = max(Max, dfs(next) + sb);}else {    Min = min(Min, dfs(next));}    }    tmp = max(Max, -Min);    return tmp;}int main() {    while (~scanf("%d%d%d", &g, &b, &s) && g || b || s) {for (int i = 0; i < b; i++) {    memset(p[i].a, 0, sizeof(p[i].a));    scanf("%d", &p[i].num);    int tmp;    for (int j = 0; j < p[i].num; j++) {scanf("%d", &tmp);p[i].a[tmp]++;    }}for (int i = 0; i < (1<<b); i++) {    vis[i] = 0;    for (int j = 0; j < b; j++) {if (i&(1<<j)) continue;for (int k = 1; k <= g; k++) {    yu[i|(1<<j)][k] = (yu[i][k] + p[j].a[k]) % s;}    }}printf("%d\n", dfs(0));    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.