思路:
题目链接http://poj.openjudge.cn/practice/C17K/
状压dp。dp[i][j]表示第i - k人到第i人的状态为j的情况下前i人中最多有多少好人。
实现:
#include <bits/stdc++.h>
using namespace std;
int dp[][];
struct node
{
int type, id1, id2;
bool f1, f2;
};
node a[];
bool same(bool a, bool b)
{
return (a && b) || (!a && !b);
}
int main()
{
int t, n, k;
cin >> t;
while (t--)
{
memset(dp, , sizeof dp);
cin >> n >> k;
string s;
getchar();
for (int i = ; i < n; i++)
{
getline(cin, s);
string tmp;
stringstream ss(s);
vector<string> v;
while (ss >> tmp) v.push_back(tmp);
if (v[][] == 'I')
{
a[i].type = ;
a[i].id1 = atoi(v[].c_str());
a[i].id2 = atoi(v[].c_str());
a[i].f1 = v[] == "good" ? true : false;
a[i].f2 = v[] == "good" ? true : false;
}
else
{
a[i].type = ;
a[i].id1 = atoi(v[].c_str());
a[i].f1 = v[] == "good" ? true : false;
}
} int msk = ( << k + ) - ;
dp[][] = ;
for (int i = ; i < n; i++)
{
memset(dp[i & ], , sizeof dp[i & ]);
for (int j = ; j < << k + ; j++)
{
if (i < k + && j >= << i) continue;
int tmp = j << & msk;
dp[i & ][tmp] = max(dp[i & ][tmp], dp[i - & ][j]);
if (a[i].type == )
{
int p1 = i - a[i].id1;
if (same(a[i].f1, j >> p1 & ))
{
dp[i & ][tmp | ] = max(dp[i & ][tmp | ], dp[i - & ][j] + );
}
}
else
{
int p1 = i - a[i].id1, p2 = i - a[i].id2;
bool g1 = j >> p1 & , g2 = j >> p2 & ;
if (!(same(a[i].f1, g1) && !same(a[i].f2, g2)))
dp[i & ][tmp | ] = max(dp[i & ][tmp | ], dp[i - & ][j] + );
}
}
}
int ans = ;
for (int i = ; i < << k + ; i++)
ans = max(ans, dp[n - & ][i]);
cout << ans << endl;
}
return ;
}