# BZOJ 2064: 分裂( 状压dp )

n1+n2次一定可以满足..然后假如之前土地集合S1的子集subs1和之后土地集合S2的子集subs2相等的话...那么就少了2个+操作...所以最后答案就是n1+n2-少掉的最多操作数, 由状压dp完成...

------------------------------------------------------------------------

#include<cstdio>

#include<cstring>
#include<algorithm>

using namespace std;

#define b(i) (1 << (i))
#define lowbit(x) ((x) & -(x))

const int maxn = 21;

int dp[b(maxn)], sum[b(maxn)];
int n, m;

void Init() {
memset(dp, 0, sizeof dp);
sum[0] = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", sum + b(i));
scanf("%d", &m);
for(int i = n; i < n + m; i++) {
scanf("%d", sum + b(i));
sum[b(i)] = -sum[b(i)];
}
}

void Dp() {
for(int S = 1, All = b(n + m); S < All; S++) {
sum[S] = sum[lowbit(S)] + sum[S ^ lowbit(S)];
for(int i = n + m; i--; )
if(S & b(i)) dp[S] = max(dp[S], dp[S ^ b(i)]);
dp[S] += !sum[S];
}
printf("%d\n", n + m - 2 * dp[b(n + m) - 1]);
}

int main() {
Init();
Dp();
return 0;
}

------------------------------------------------------------------------

## 2064: 分裂

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 252  Solved: 156
[Submit][Status][Discuss]

1 6
3 1 2 3

2

## Source

posted @ 2015-11-22 21:39  JSZX11556  阅读(210)  评论(0编辑  收藏  举报