题目描述

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个数,从中任意选出一些数,使这些数能分成和相等的两组。

求有多少种选数的方案。

输入输出格式

输入格式:

* Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains M(i).

输出格式:

* Line 1: The number of balanced subsets of cows.

输入输出样例

输入样例#1: 复制

4
1
2
3
4
输出样例#1: 复制

3

说明

There are 4 cows, with milk outputs 1, 2, 3, and 4.

There are three balanced subsets: the subset {1,2,3}, which can be partitioned into {1,2} and {3}, the subset {1,3,4}, which can be partitioned into {1,3} and {4}, and the subset {1,2,3,4} which can be partitioned into {1,4} and {2,3}.

思路:

第一遍gg,敲完交上以后发现读错题了。。。然后就急匆匆的写了个暴力。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int num[];
int n,ans;
void dfs(int now,int lsum,int rsum,int tot){
if(lsum==rsum&&tot!=){ ans++;return ; }
if(now==n+) return ;
dfs(now+,lsum+num[now],rsum,tot+);
dfs(now+,lsum,rsum+num[now],tot+);
dfs(now+,lsum,rsum,tot);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&num[i]);
sort(num+,num++n);
dfs(,,,);
cout<<ans/;
}

19分暴力,不兹道为什么wa了3个点

思路:meet int the middle,不知道为什么,总有一个点卡不过去,只有开开O2优化才能过。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 21
using namespace std;
int n,nn,ans,numl,numr;
int num[],vis[<<MAXN];
struct nond{ int sum,stat; }lft[<<MAXN],rght[<<MAXN];
int cmp1(nond a,nond b){ return a.sum<b.sum; }
int cmp2(nond a,nond b){ return a.sum>b.sum; }
void read(int &x){
x=;int f=;char c=getchar();
while(c<''&&c>''){ if(c=='-') f=-;c=getchar(); }
while(c>=''&&c<=''){ x=x*+c-'';c=getchar(); }
x*=f;
}
void dfs(int now,int tot,int val,int nowstact){
if(now>tot){
if(tot==nn){ lft[++numl].sum=val;lft[numl].stat=nowstact; }
else{ rght[++numr].sum=val;rght[numr].stat=nowstact; }
return ;
}
dfs(now+,tot,val,nowstact);
dfs(now+,tot,val-num[now],nowstact+(<<(now-)));
dfs(now+,tot,val+num[now],nowstact+(<<(now-)));
}
int main(){
scanf("%d",&n);nn=n/;
for(int i=;i<=n;i++) scanf("%d",&num[i]);
dfs(,nn,,);sort(lft+,lft++numl,cmp1);
dfs(nn+,n,,);sort(rght+,rght++numr,cmp2);
int l=,r=;
while(l<=numl&&r<=numr){
while(lft[l].sum+rght[r].sum>&&r<=numr) r++;
int pre=r;
while(lft[l].sum+rght[r].sum==&&r<=numr){
if(!vis[lft[l].stat|rght[r].stat]){ vis[lft[l].stat|rght[r].stat]=;ans++; }
r++;
}
if(lft[l].sum==lft[l+].sum) r=pre; l++;
}
printf("%d",ans-);
}

洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…的更多相关文章

  1. 折半搜索+状态压缩【P3067】 [USACO12OPEN]平衡的奶牛群Balanced Cow S…

    Description 给n个数,从中任意选出一些数,使这些数能分成和相等的两组. 求有多少种选数的方案. Input 第\(1\)行:一个整数\(N\) 第\(2\)到\(N+1\)行,包含一个整数 ...

  2. [luogu3067 USACO12OPEN] 平衡的奶牛群

    传送门 Solution 折半搜索模板题 考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可 Code //By Menteur_Hxy #include <cmath> #i ...

  3. 洛谷 P4016负载平衡问题【费用流】题解+AC代码

    洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...

  4. (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币

    bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...

  5. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  6. 洛谷P3067 平衡的奶牛群 [USACO12OPEN] meet-in-the-middle

    正解:搜索 解题报告: 先放下传送门QwQ 这题就,双向搜索经典题鸭 首先dfs应该挺好想到的我jio得?就是我们不用记录左右分别得分多少只要记下差值就好了嘛能get? 然后就先搜左边,记录下每个得分 ...

  7. 洛谷 [P4016] 负载平衡问题

    贪心做法 第一眼看见觉得和均分纸牌差不多,然而因为这是环形的,并不能用均分纸牌的方法做,但是均分纸牌的思想仍然适用 首先我们假设平均数为sum1. 那么对于第1个人,我们假设他给第N个人K个糖果, 第 ...

  8. 洛谷P4016负载平衡

    题目 负载平衡问题是一个比较经典的网络流问题,但是该问题还有一个数学贪心法. 所以做这个题前,其实可以做一下均分纸牌问题. 均分纸牌问题 均分纸牌问题可以说是作为贪心的入门题. 做法 首先我们应当把原 ...

  9. 洛谷P4104 [HEOI2014]平衡(dp 组合数学)

    题意 题目链接 Sol 可以把题目转化为从\([1, 2n + 1]\)中选\(k\)个数,使其和为\((n+1)k\). 再转化一下:把\((n+1)k\)划分为\(k\)个数,满足每个数在范围在\ ...

随机推荐

  1. Mechanize抓取数据【Ruby】

    创建: 2017/08/05 更新: 2018/01/08 修正: ele_inner_text -> ele.inner_text                           补充: ...

  2. [Swift通天遁地]四、网络和线程-(4)使用Alamofire实现网络请求

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. mysql中类型转换

    MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值 CAST(xxx AS 类型), CONVERT(xxx,类型) 二进制,同带binary前缀的效果 : ...

  4. sql数据库中常用连接

    很简单的知识点,今天有点搞不清楚左外连接,右外连接:详见以下: --表stu id name 1, Jack 2, Tom 3, Kity 4, nono --表exam id grade 1, 56 ...

  5. nginx深入

    1.编译安装配置完成 /opt/nginx11/html/index.html 这是网页的首页文件 2. nginx.conf主配置文件学习 ############################# ...

  6. c#异步多线程

    1.asyncrel = delegate.BeginInvoke实现委托异步调用. 2.异步等待 asyncrel.IsCompleted用于判断是否执行完毕 or EndInvoke用于等待执行完 ...

  7. Java系列学习(七)-面向对象

    1.成员变量和局部变量的区别 (1)在类中的位置不同 成员变量:类中 方法外 局部变量:方法中 (2)在内存中的位置不同 成员变量:在堆中 局部变量:在栈中 (3)生命周期不同 成员变量:随着对象的创 ...

  8. [ CQOI 2009 ] 中位数图

    \(\\\) \(Description\) 给出\(N\)的一个全排列,统计该排列有多少个长度为奇数的连续子序列,中位数是\(B\). \(N\in [0,10^5]\),\(B\in [0,N]\ ...

  9. HTML TabIndex属性

    TabIndex作用: tabindex:全局属性.指示其元素是否可以聚焦(获得焦点),以及它是否/在何处参与顺序键盘导航(因通常使用tab键操作,顾因此得名). 当使用tab键在网页控件中进行导航时 ...

  10. js 翻页

    翻页功能是js很基础的一个算法,且用得很多,所以必须掌握此项技能. 我们要想清楚在实现翻页的过程中需要哪几个步骤: 1.我们首先需要的变量有哪些,必须的有一个存放当前页码的变量nowPage.一个存放 ...