BZOJ

洛谷

\(Description\)

给定\(n\)个数\(A_i\)。求它有多少个子集,满足能被划分为两个和相等的集合。

\(n\leq 20,1\leq A_i\leq10^8\)。

\(Solution\)

显然我们要预处理哪些集合可以被划分为两个和相等的集合。每个元素三种状态,这样我们就可以得到一个\(O(3^n)\)的做法。。

显然不行啊,但是和相等这种东西可以折半啊!

将序列分成两半分开DFS。这样两个和相等的集合\(S_1,S_2\)中的元素可能会被分开。设\(a\)为\(S_1\)在前一半中序列的元素的和,\(b\)为\(S_1\)在后一半序列中的元素的和;\(c,d\)分别为\(S_2\)在前一半/后一半序列中元素的和。那么有\(a+b=c+d\to a-c=d-b\)。所以我们统计两半序列中哪些\(a-c\)相等的集合就可以了。

和为\(a-c\)的集合可能有多个,直接\(Hash/map+vector\)存一下有哪些集合就可以了(这题集合可以直接二进制状压)。

复杂度是不是还会被卡到\(O(6^{\frac n2})\)啊。。在SPOJ是T了,但能过BZOJ。

其实合并状态可以sort后线性合并,就快很多并且能过了。(不对啊,感觉复杂度差不多啊==,我hash写太丑了?)

当然这也不是正解,还有更快的,比如:https://blog.csdn.net/u014609452/article/details/51872702

//7132kb	2584ms
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define mod 20000000
typedef long long LL;
const int N=61005;//为啥60005在BZOJ上RE啊,自测可过。 int n,mid,cnt,A[23],Enum,H[(1<<20)+5],sum[N],nxt[N];
bool vis[(1<<20)+5];
struct Node
{
int s,sum;
bool operator <(const Node &x)const
{
return sum<x.sum;
}
}rs[N]; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline void AE(int s,int Sum)
{
sum[++Enum]=Sum, nxt[Enum]=H[s], H[s]=Enum;
}
void DFS1(int x,int s,int sum)
{
if(x<0) {AE(s,sum); return;}
DFS1(x-1,s,sum), DFS1(x-1,s|(1<<x),sum+A[x]), DFS1(x-1,s|(1<<x),sum-A[x]);
}
void DFS2(int x,int s,int sum)
{
if(x==n) {rs[++cnt]=(Node){s,sum}; return;}
DFS2(x+1,s,sum), DFS2(x+1,s|(1<<x),sum+A[x]), DFS2(x+1,s|(1<<x),sum-A[x]);
} int main()
{
static int lsum[N];
n=read(),mid=n>>1;
for(int i=0; i<n; ++i) A[i]=read();
DFS1(mid-1,0,0), DFS2(mid,0,0);
std::sort(rs+1,rs+1+cnt);
for(int s=0,l=1<<mid; s<l; ++s)
{
int t=0;
for(int i=H[s]; i; i=nxt[i]) lsum[++t]=sum[i];
std::sort(lsum+1,lsum+1+t);
for(int i=1,now=1; i<=cnt; ++i)
{
while(now<=t && lsum[now]<rs[i].sum) ++now;
if(now>t) break;
if(lsum[now]==rs[i].sum) vis[rs[i].s|s]=1;
}
}
int ans=0;
for(int i=1,l=1<<n; i<l; ++i) ans+=vis[i];//同一个集合会算重啊
printf("%d\n",ans); return 0;
}

在SPOJ上T掉的:

//89344kb	7060ms
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define mod 20000000
typedef long long LL;
const int N=61005; int n,mid,A[23];
bool vis[(1<<20)+5];
struct Hash_Table
{
int delta,Enum,H[mod+2],nxt[N],s[N]; LL sum[N];//可能冲突 再存一下sum
inline void Insert(int S,LL Sum)
{//注意和可能是负的(加一个mod不就好了==)
int x=(Sum+delta)%mod;
s[++Enum]=S, sum[Enum]=Sum, nxt[Enum]=H[x], H[x]=Enum;
}
inline void Solve(int S,LL Sum)
{
int x=(Sum+delta)%mod;
for(int i=H[x]; i; i=nxt[i])
if(sum[i]==Sum) vis[S|s[i]]=1;
}
}hs; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
void DFS1(int x,int s,LL sum)
{
if(x==mid) {hs.Insert(s,sum); return;}
DFS1(x+1,s,sum), DFS1(x+1,s|(1<<x),sum+A[x]), DFS1(x+1,s|(1<<x),sum-A[x]);
}
void DFS2(int x,int s,LL sum)
{
if(x==n) {hs.Solve(s,sum); return;}
DFS2(x+1,s,sum), DFS2(x+1,s|(1<<x),sum+A[x]), DFS2(x+1,s|(1<<x),sum-A[x]);
} int main()
{
n=read(),mid=n>>1; int s=0;
for(int i=0; i<n; ++i) s+=A[i]=read();
hs.delta=s, DFS1(0,0,0), DFS2(mid,0,0);
int ans=0;
for(int i=1,l=1<<n; i<l; ++i) ans+=vis[i];//同一个集合会算重啊
printf("%d\n",ans); return 0;
}

BZOJ.2679.Balanced Cow Subsets(meet in the middle)的更多相关文章

  1. 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)

    [Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...

  2. 折半搜索+Hash表+状态压缩 | [Usaco2012 Open]Balanced Cow Subsets | BZOJ 2679 | Luogu SP11469

    题面:SP11469 SUBSET - Balanced Cow Subsets 题解: 对于任意一个数,它要么属于集合A,要么属于集合B,要么不选它.对应以上三种情况设置三个系数1.-1.0,于是将 ...

  3. BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针

    BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针 Description Farmer John's owns N ...

  4. bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)

    2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 462  Solv ...

  5. 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets

    [算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...

  6. [Usaco2012 Open]Balanced Cow Subsets

    Description Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk ...

  7. BZOJ2679 : [Usaco2012 Open]Balanced Cow Subsets

    考虑折半搜索,每个数的系数只能是-1,0,1之中的一个,因此可以先通过$O(3^\frac{n}{2})$的搜索分别搜索出两边每个状态的和以及数字的选择情况. 然后将后一半的状态按照和排序,$O(2^ ...

  8. bzoj2679:[Usaco2012 Open]Balanced Cow Subsets

    思路:折半搜索,每个数的状态只有三种:不选.选入集合A.选入集合B,然后就暴搜出其中一半,插入hash表,然后再暴搜另一半,在hash表里查找就好了. #include<iostream> ...

  9. SPOJ-SUBSET Balanced Cow Subsets

    嘟嘟嘟spoj 嘟嘟嘟vjudge 嘟嘟嘟luogu 这个数据范围都能想到是折半搜索. 但具体怎么搜呢? 还得扣着方程模型来想:我们把题中的两个相等的集合分别叫做左边和右边,令序列前一半中放到左边的数 ...

随机推荐

  1. python网络爬虫笔记(九)

    4.1.1 urllib2 和urllib是两个不一样的模块 urllib2最简单的就是使用urllie2.urlopen函数使用如下 urllib2.urlopen(url[,data[,timeo ...

  2. hdu6273 线性差分

    #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ; LL a[maxn],b[maxn]; LL ...

  3. CHENGDU3-Restful API 接口规范、django-rest-framework框架

    Restful API 接口规范.django-rest-framework框架 问题:什么是API? 答:API是接口,提供url. 接口有两个用途: 为别人提供服务,前后端分离. 为什么使用前后端 ...

  4. (转)HTTPS到底是个啥玩意儿?

    详细见:https://blog.csdn.net/zgwangbo/article/details/50889623 ,建立交互的过程见下图:

  5. github协作开发遇到的问题

    1.十一来了,帝都不好买票,30号就调休一天回去了,项目还没搞完,紧张的不行,就自己和同事搞了一个github协作开发,由于是功能和公司项目不是很沾边,但是是自己的主要工作,就和同事协调了一下,搭建了 ...

  6. [转] 使用babel-plugin-react-css-modules简化CSS Modules的使用

    在我们的产品中,均使用CSS Modules来作为样式解决方案,大致的代码是这样的: import React from 'react'; import styles from './table.cs ...

  7. ajax请求头加Token时发生的跨域(CORS)请求问题

    首先描述下问题:需求是在请求头中加入token,我在ajax请求数据时添加了请求头‘Authorization’字段,并添加了响应的token值,在请求数据的时候浏览器报错如下: Request he ...

  8. (2).NET CORE微服务 Micro-Service ---- .NetCore启动配置 和 .NetCoreWebApi

    什么是.Net Core?.Net Core是微软开发的另外一个可以跨Linux.Windows.mac等平台的.Net.Net Core相关知识看文章地步dotnet dllname.dll 运行P ...

  9. Evaluation map and reflexive space

    For a normed space \(X\), an isometric isomorphism can be defined from \(X\) to its second dual spac ...

  10. [转]Java Web笔记:搭建环境和项目配置(MyEclipse 2014 + Maven + Tomcat)

    来源:http://www.jianshu.com/p/56caa738506a 0. 绪言 Java Web开发中,除了基础知识外,开发环境搭建,也是一项基本功.开发环境包括了IDE.项目管理.项目 ...