[Usaco2012 Open]Balanced Cow Subsets
Description
Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk each day (1 <= M(i) <= 100,000,000). FJ wants to streamline the process of milking his cows every day, so he installs a brand new milking machine in his barn. Unfortunately, the machine turns out to be far too sensitive: it only works properly if the cows on the left side of the barn have the exact same total milk output as the cows on the right side of the barn! Let us call a subset of cows "balanced" if it can be partitioned into two groups having equal milk output. Since only a balanced subset of cows can make the milking machine work, FJ wonders how many subsets of his N cows are balanced. Please help him compute this quantity.
给出N(1≤N≤20)个数M(i) (1 <= M(i) <= 100,000,000),在其中选若干个数,如果这几个数可以分成两个和相等的集合,那么方案数加1。问总方案数。
Input
Line 1: The integer N.
Lines 2..1+N: Line i+1 contains M(i).
Output
Line 1: The number of balanced subsets of cows.
Sample Input
4 1 2 3 4
Sample Output
3
直接搜复杂度\(O(3^n)\),显然不行,考虑折半搜索,分成两部分,这样复杂度变为\(O(2*3^{n/2})\),然后对两部分进行查找即可,细节见代码
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=20,M=6e4;
struct S1{
int val,sta;
void insert(int v,int s){val=v,sta=s;}
}A[M+10],B[M+10];
int v[N+10],cntA,cntB,n;
bool vis[(1<<N)+10];
bool cmp1(const S1 &x,const S1 &y){return x.val<y.val;}
bool cmp2(const S1 &x,const S1 &y){return x.val>y.val;}
void dfs(int x,int limit,int sta,int sum){
if (x>limit){
limit==n>>1?A[++cntA].insert(sum,sta):B[++cntB].insert(sum,sta);
return;
}
dfs(x+1,limit,sta,sum);
dfs(x+1,limit,sta|(1<<(x-1)),sum+v[x]);
dfs(x+1,limit,sta|(1<<(x-1)),sum-v[x]);
}
int main(){
n=read();
for (int i=1;i<=n;i++) v[i]=read();
dfs(1,n>>1,0,0),dfs((n>>1)+1,n,0,0);
sort(A+1,A+1+cntA,cmp1);
sort(B+1,B+1+cntB,cmp2);
int i=1,j=1,Ans=0;
while (i<=cntA&&j<=cntB){
while (j<=cntB&&-B[j].val<A[i].val) j++;
int tmp=j;
while (A[i].val+B[j].val==0){
if (!vis[A[i].sta|B[j].sta]) vis[A[i].sta|B[j].sta]=1,Ans++;
j++;
}
j=tmp,i++;
}
printf("%d\n",Ans-1);
}
[Usaco2012 Open]Balanced Cow Subsets的更多相关文章
- 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)\) ...
- bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)
2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 462 Solv ...
- 折半搜索+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^ ...
- bzoj2679:[Usaco2012 Open]Balanced Cow Subsets
思路:折半搜索,每个数的状态只有三种:不选.选入集合A.选入集合B,然后就暴搜出其中一半,插入hash表,然后再暴搜另一半,在hash表里查找就好了. #include<iostream> ...
- 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets
[算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...
- BZOJ.2679.Balanced Cow Subsets(meet in the middle)
BZOJ 洛谷 \(Description\) 给定\(n\)个数\(A_i\).求它有多少个子集,满足能被划分为两个和相等的集合. \(n\leq 20,1\leq A_i\leq10^8\). \ ...
- SPOJ-SUBSET Balanced Cow Subsets
嘟嘟嘟spoj 嘟嘟嘟vjudge 嘟嘟嘟luogu 这个数据范围都能想到是折半搜索. 但具体怎么搜呢? 还得扣着方程模型来想:我们把题中的两个相等的集合分别叫做左边和右边,令序列前一半中放到左边的数 ...
随机推荐
- 【Git使用具体解释】Egit的经常使用操作具体解释
经常使用操作 操作 说明 Fetch 从远程获取最新版本号到本地,不会自己主动merge Merge 能够把一个分支标签或某个commit的改动合并如今的分支上 Pull 从远程获取最新版本号并mer ...
- How can we listen for errors that do not trigger window.onerror?
原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-win ...
- AFNetworking配合Swift3.0请求数据
首先用桥接或pods将AFNetworking导入项目,在这不再赘述,然后创建一个单例NetWorkTools.swift 继承:AFHTTPSessionManager import UIKit i ...
- 解决cell切割线不是全屏问题
if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) { [_tableView setSeparatorInset:U ...
- 发挥bat的作用
from 转自:http://blog.csdn.net/hitlion2008/article/details/7467252 1.什么是Windows BATCH BATCH也就是批处理文件,有时 ...
- ExtJs中多个form情况下指定某个form使能
采用extjs的时候,如果一个页面存在多个,那么提交之时,究竟是哪个form使能,就要指明.我今天就遇到了这种情况:明明页面已经有提交,为啥没有提交到内容?一查才知道,我的页面是有2个form,我本意 ...
- 2016/05/05 smarty ① 登录 ②主页面 ③删除 ④让缩略信息显示完整 (补:增加 修改 )
共 八个页面 ①login.php <?php include("init.inc.php"); $smarty->display("login.html& ...
- hosts所在文件夹以及***
hosts所在文件夹: Windows 系统hosts位于 C:\Windows\System32\drivers\etc\hosts Android(安卓)系统hosts位于 /etc/hosts ...
- CRM 配置 ADFS后,使用自定义STS遇到的问题总结
1 登录ADFS服务查看 ADFS日志 2 根据日志提示的错误,设置ADFS对应的属性 (Get-ADFSRelyingPartyTrust) | Set-ADFSRelyingPartyTrust ...
- mongodb05---游标
游标cursor: 通俗的说,游标不是查询结果,而是查询的返回资源,或者接口. 通过这个接口,你可以逐条读取.就像php中的fopen打开文件,得到一个资源一样, 通过资源,可以一行一行的读文件. v ...