2013 杭州 onsite I 题
总和一定的博弈,用dp解决
类似的题目:
uva一题:
http://blog.youkuaiyun.com/guognib/article/details/13168081
2013年网络赛南京赛区的一题:
http://blog.youkuaiyun.com/guognib/article/details/13956453
这类的题目经常需要一些预处理,剩下就几乎是一样的思路
大致是:dp [s] = newleft - dp[s ^ (1 << i)]
高效率递推法:http://blog.youkuaiyun.com/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;
}