[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 这个数据范围都能想到是折半搜索. 但具体怎么搜呢? 还得扣着方程模型来想:我们把题中的两个相等的集合分别叫做左边和右边,令序列前一半中放到左边的数 ...
随机推荐
- CoolCTO - 创业者的技术合伙人
CoolCTO - 创业者的技术合伙人
- 查看MySQL系统变量的命令
用了好长时间mysql,却没有用心记住一些有用的东西,加油! mysql> SHOW VARIABLES; +---------------------------------+-------- ...
- C++ std::tr1::bind使用
1. 简述 同function函数相似.bind函数相同也能够实现相似于函数指针的功能.但却却比函数指针更加灵活.特别是函数指向类 的非静态成员函数时.std::tr1::function 能够对静态 ...
- [RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
switchMap is mergeMap that checks for an "inner" subscription. If the "inner" su ...
- onfocus事件,onblur事件;Focus()方法,Blur()方法
<1> <pre name="code" class="html"><!DOCTYPE html PUBLIC "-// ...
- Scrum 每日站会
站立式会议(Daily Scrum, 有时候我们直接叫做Daily Meeting)是Scrum敏捷软件开发方法学的实践之一,也是团队最容易实施的敏捷实践,实施成本低. 具体做法,团队成员每天固定时间 ...
- Android中View窗口getWidth和getMeasuredWidth的差别
今天在研究自己定义listview的下拉刷新的效果.想移植到项目需求中,再看自己定义源代码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么差别,求教万能的百度,经调 ...
- 2016/04/29 ①cms分类 ② dede仿站制作 步骤 十个步骤 循环生成菜单 带子菜单的菜单 标签 栏目 栏目内容列表 内容图片列表
cms 系统还有: phpcms 企业站 Xiaocms 织梦 企业站 wordpress (博客) Ecshop 商城 Ecmall 多用户 Discms 记账 方维 订餐 团购 CMS ...
- zTree 基本用法
[简介] zTree 是利用 JQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同时生成多个 Tree 实例 ...
- UVa 12403 - Save Setu
题目:有两种操作:1.当前数值添加.2.输出当前数值. 分析:简单题.模拟就可以. 说明:A+B. #include <iostream> #include <cstdlib> ...