bzoj2679:[Usaco2012 Open]Balanced Cow Subsets
思路:折半搜索,每个数的状态只有三种:不选、选入集合A、选入集合B,然后就暴搜出其中一半,插入hash表,然后再暴搜另一半,在hash表里查找就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 25 int n;
int a[maxn],t[maxn];
bool v[1025][1025];
int ans; struct hash_table{
static const int mod=242354;
int tot,now[mod],pre[1000000],son[1000000],val[1000000];
void insert(int t,int x){
int pos=(x%mod+mod)%mod;
for (int p=now[pos];p;p=pre[p]) if (son[p]==t&&val[p]==x) return;
son[++tot]=t,val[tot]=x,pre[tot]=now[pos],now[pos]=tot;
}
int find(int t,int x){
int pos=(x%mod+mod)%mod,ans=0;
for (int p=now[pos];p;p=pre[p])
if (val[p]==x&&!v[son[p]][t]){v[son[p]][t]=1;ans++;}
return ans;
}
}H; void dfs1(int x,int val){
if (x>n/2){
int tmp=0;for (int i=1;i<=n/2;i++) tmp=tmp<<1|t[i];
H.insert(tmp,val);return;
}
t[x]=1;dfs1(x+1,val+a[x]);
t[x]=0;dfs1(x+1,val);
t[x]=1;dfs1(x+1,val-a[x]);
} void dfs2(int x,int val){
if (x>n){
int tmp=0;for (int i=n/2+1;i<=n;i++) tmp=tmp<<1|t[i];
ans+=H.find(tmp,val);
return;
}
t[x]=1;dfs2(x+1,val+a[x]);
t[x]=0;dfs2(x+1,val);
t[x]=1;dfs2(x+1,val-a[x]);
} int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs1(1,0),dfs2(n/2+1,0);
printf("%d\n",ans-1);
return 0;
}
bzoj2679:[Usaco2012 Open]Balanced Cow Subsets的更多相关文章
- bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)
2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 462 Solv ...
- 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 ...
- 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)
[Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...
- 折半搜索+Hash表+状态压缩 | [Usaco2012 Open]Balanced Cow Subsets | BZOJ 2679 | Luogu SP11469
题面:SP11469 SUBSET - Balanced Cow Subsets 题解: 对于任意一个数,它要么属于集合A,要么属于集合B,要么不选它.对应以上三种情况设置三个系数1.-1.0,于是将 ...
- BZOJ2679 : [Usaco2012 Open]Balanced Cow Subsets
考虑折半搜索,每个数的系数只能是-1,0,1之中的一个,因此可以先通过$O(3^\frac{n}{2})$的搜索分别搜索出两边每个状态的和以及数字的选择情况. 然后将后一半的状态按照和排序,$O(2^ ...
- [Usaco2012 Open]Balanced Cow Subsets
Description Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk ...
- 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets
[算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...
- 「题解」:毛一琛/$cow$ $subsets$
问题 A: 毛一琛/$cow$ $subsets$ 时间限制: 1 Sec 内存限制: 512 MB 题面 题面谢绝公开. 题解 题名貌似是个大神??看起来像是签到题然后就死了. 首先$O(3^n) ...
- BZOJ.2679.Balanced Cow Subsets(meet in the middle)
BZOJ 洛谷 \(Description\) 给定\(n\)个数\(A_i\).求它有多少个子集,满足能被划分为两个和相等的集合. \(n\leq 20,1\leq A_i\leq10^8\). \ ...
随机推荐
- HW4.40
public class Solution { public static void main(String[] args) { long positiveSide = 0; long negativ ...
- iOS 第三方库冲突的处理
最近项目组在做一些第三方功能的集成,不止一次的遇到第三方库冲突的问题,报错如下: duplicate symbol _OBJC_METACLASS_$_JKSerializer in: /Users/ ...
- glusterfs repo
Installing Gluster For RPM based distributions, if you will be using InfiniBand, add the glusterfs R ...
- PC-博客首页中增加必应或GOOGLE搜索功能
<script type="text/javascript" language="javascript"> function SearchGoogl ...
- Android UI开发第三十二篇——Creating a Navigation Drawer
Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...
- hdu 2102 A计划(优先队列+dfs)
改了好久,上午来实验室打出来了,运行就是不对,一直找啊找!还是没找到,最后突然停电了,打好的代码还没保存呢! 刚才来的时候又重新打了一遍!!!结果一个小小的错误wrong了好久!!! 在dfs值返回时 ...
- PTA 5-12 How Long Does It Take (25分)
这题看不太懂题目啊~ 参考的http://blog.csdn.net/qq_26437925/article/details/49420089?locationNum=6&fps=1 先放着 ...
- (二 )VMware workstation 部署虚拟集群实践——并行批量操作环境部署
在上一篇博客中,已经介绍了安装虚拟集群的过程和需要注意的细节问题. 这篇主要是介绍如何批量登陆远程主机和配置,这个过程中是在没有部署并行处理工具或者集群管理工具的前进行的. ------------首 ...
- [React] React Fundamentals: Add-on ClassSet() for ClassName
To get the add-ons, use react-with-addons.js (and its minified counterpart) rather than the common r ...
- Kinect for Windows V2和V1对照开发___深度数据获取并用OpenCV2.4.10显示
V1深度分辨率:320x240 V2深度分辨率:512x424 1. 打开深度图像帧的方式 对于V1: hr = m_PNuiSensor->NuiImageStreamOpen( NUI_I ...