最坏的情况是全部合起来然后拆分,n+m-2
每找到一个和相等的子集,答案-2
问题转化为找到最多的子集
转化为随机序列前缀和相等
#include<cstdio> #include<algorithm> using namespace std; int a[15],b[15],Suma[2005],Sumb[2005],F[2005][2005],ID[2005]; int lowbit(int x){ return x&(-x); } int main(){ int n,m; scanf("%d",&n); for (int i=0; i<n; i++) scanf("%d",&a[i]); scanf("%d",&m); for (int i=0; i<m; i++) scanf("%d",&b[i]); for (int i=0; i<max(n,m); i++) ID[1<<i]=i; for (int i=1; i<(1<<n); i++) Suma[i]=Suma[i^lowbit(i)]+a[ID[lowbit(i)]]; for (int i=1; i<(1<<m); i++) Sumb[i]=Sumb[i^lowbit(i)]+b[ID[lowbit(i)]]; for (int i=0; i<(1<<n); i++) for (int j=0; j<(1<<m); j++){ if (Suma[i]==Sumb[j]) F[i][j]++; for (int k=0; k<n; k++) if (!(i&(1<<k))) F[i|(1<<k)][j]=max(F[i|(1<<k)][j],F[i][j]); for (int k=0; k<m; k++) if (!(j&(1<<k))) F[i][j|(1<<k)]=max(F[i][j|(1<<k)],F[i][j]); } printf("%d ",n+m+2-2*F[(1<<n)-1][(1<<m)-1]); return 0; }