hdu 4778 Gems Fight!(2013 杭州 现场赛 I 题) 总和一定的博弈,dp

2013 杭州 onsite I 题

总和一定的博弈,用dp解决 

类似的题目:

uva一题:

http://blog.csdn.net/guognib/article/details/13168081

2013年网络赛南京赛区的一题:

http://blog.csdn.net/guognib/article/details/13956453

这类的题目经常需要一些预处理,剩下就几乎是一样的思路

大致是:dp [s] = newleft - dp[s ^ (1 << i)]

高效率递推法:http://blog.csdn.net/auto_ac/article/details/14800835

//#pragma warning (disable: 4786)
//#pragma comment (linker, "/STACK:16777216")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define CPY(a, b) memcpy(a, b, sizeof(a))
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;

int ALL;
int b[23][10];
int B, G, S;
int sum[1 << 21];
int dp[1 << 21];
int vis[1 << 21];

void init()
{
    int d[10];
    CLR(sum, 0);
    for (int ss = 0; ss <= ALL; ss++)
    {
        CLR(d, 0);
        for (int r = 0; r < B; r++)
            if (ss & (1 << r))
                for (int j = 0; j < G; j++)
                    d[j] += b[r][j];
        for (int j = 0; j < G; j++)
        sum[ss] += d[j] / S;
    }
}

int dpf(int s, int c[], int left)
{
    if (vis[s]) return dp[s];
    vis[s] = 1;
    int &ans = dp[s];
    ans = 0;
    int d[10];
    int x;
    REP(i, B)
    {
        if (s & (1 << i))
        {
            int num = 0;
            REP(j, G)
            {
                x = c[j] + b[i][j];
                num += x / S;
                d[j] = x % S;
            }
            if (num) ans = max(ans, num + dpf(s ^ (1 << i), d, left - num));
            else ans = max(ans, left - dpf(s ^ (1 << i), d, left));
        }
    }
    return ans;
}

int main ()
{
    int c[10];
    int x, xn;
    while (~scanf("%d%d%d", &G, &B, &S) && B + G + S)
    {
        CLR(b, 0);
        REP(i, B)
        {
            scanf("%d", &xn);
            REP(j, xn)
            {
                scanf("%d", &x); --x;
                b[i][x]++;
            }
        }
        ALL = (1 << B) - 1;
        init();
        CLR(c, 0);
        CLR(vis, 0);
        vis[0] = 1; dp[0] = 0;
        printf("%d\n", 2 * dpf(ALL, c, sum[ALL]) - sum[ALL]);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值