# bzoj2064[和谐社会模拟赛]分裂

#include<cstdio>

#include<cstring>

#include<cassert>

const int inf=0x3f3f3f3f;

inline int lowbit(int x){

return x&(-x);

}

inline int min(int a,int b){

return a<b?a:b;

}

int a[15],b[15];

int suma[1024],sumb[1024];

int f[1024][1024];

int g[1024];

const int maxn=10005;

int u[maxn],v[maxn];

int main(){

int n,m;

scanf("%d",&n);

for(int i=0;i<n;++i){

scanf("%d",a+i);

suma[1<<i]=a[i];

}

scanf("%d",&m);

for(int i=0;i<m;++i){

scanf("%d",b+i);

sumb[1<<i]=b[i];

}

int lim1=1<<n,lim2=1<<m;

for(int i=1;i<lim1;++i){

suma[i]=suma[i^lowbit(i)]+suma[lowbit(i)];

}

for(int i=1;i<lim2;++i){

sumb[i]=sumb[i^lowbit(i)]+sumb[lowbit(i)];

}

for(int i=1;i<1024;++i)g[i]=g[i>>1]+(i&1);

int cnt=0;

memset(f,0x3f,sizeof(f));

for(int i=1;i<lim1;++i){

for(int j=1;j<lim2;++j){

if(suma[i]==sumb[j]){

u[++cnt]=i;

v[cnt]=j;

f[i][j]=g[i]+g[j]-2;

}

}

}

for(int i=1;i<=cnt;++i){

for(int j=i+1;j<=cnt;++j){

if((u[i]&u[j])||(v[i]&v[j])){

continue;

}

f[u[i]|u[j]][v[i]|v[j]]=min(f[u[i]|u[j]][v[i]|v[j]],f[u[i]][v[i]]+f[u[j]][v[j]]);

}

}

assert(cnt<=8000);

printf("%d\n",f[lim1-1][lim2-1]);

return 0;

}

#include<cstdio>

#include<cstring>

const int inf=0x3f3f3f3f;

inline int lowbit(int x){

return x&(-x);

}

inline int max(int a,int b){

return a>b?a:b;

}

int a[15],b[15];

int suma[1024],sumb[1024];

bool e[1024][1024];

int f[1024][1024];

int g[1024];

int main(){

int n,m;

scanf("%d",&n);

for(int i=0;i<n;++i){

scanf("%d",a+i);

suma[1<<i]=a[i];

}

scanf("%d",&m);

for(int i=0;i<m;++i){

scanf("%d",b+i);

sumb[1<<i]=b[i];

}

int lim1=1<<n,lim2=1<<m;

for(int i=1;i<lim1;++i){

suma[i]=suma[i^lowbit(i)]+suma[lowbit(i)];

}

for(int i=1;i<lim2;++i){

sumb[i]=sumb[i^lowbit(i)]+sumb[lowbit(i)];

}

for(int i=1;i<lim1;++i){

for(int j=1;j<lim2;++j){

for(int k=0;k<n;++k){

if(i&(1<<k))f[i][j]=max(f[i][j],f[i^(1<<k)][j]);

}

for(int k=0;k<m;++k){

if(j&(1<<k))f[i][j]=max(f[i][j],f[i][j^(1<<k)]);

}

if(suma[i]==sumb[j])f[i][j]++;

}

}

printf("%d\n",n+m-2*f[lim1-1][lim2-1]);

return 0;

}



posted @ 2016-11-01 14:52  liu_runda  阅读(807)  评论(0编辑  收藏  举报