题目描述

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. Django返回json给钱前台的方法

    return HttpResponse(simplejson.dumps(resource.update_status, ensure_ascii=False))

  2. mysql索引的操作

    一.创建和查看普通索引 这是最基本的索引类型,而且它没有唯一性之类的限制 1.创建表时创建普通索引 CREATE TABLE table_name( 属性名 数据类型, ... 属性名 数据类型, I ...

  3. springboot自定义常量配置

    现在你建一个类: import org.springframework.boot.context.properties.ConfigurationProperties; /** * Created b ...

  4. [ POI 2011 ] Dynamite

    \(\\\) \(Description\) 一棵\(N\)个节点的树,树上有\(M\)个节点是关键点,选出\(K\)个特殊点,使得所有关键点到特殊点的距离中最大的最小,输出最大值最小为多少. \(N ...

  5. 笔记《精通css》第3章 盒模型,定位,浮动,清理

    第3章    盒模型,定位,浮动,清理 1.盒模型用到的属性width,height,padding,border,margin 普通文档流的上下垂直margin会叠加 2.块级框 与 行内框, 利用 ...

  6. Linux(centOS7.2)+node+express初体验

    赶着阿里云服务器老用户服务器半折的好时机,手痒买了一个低配. 想着对于低配用Linux应该比较好(无可视化界面) 于是选择安装了centOs7.2: 我是通过SecureCRT进行远程连接的(如何操作 ...

  7. CSS——层叠性

    层叠性:浏览器渲染是从上而下的,当多个样式作用于同一个(同一类)标签时,样式发生了冲突,总是执行后边的代码(后边代码层叠前边的代码).和标签调用选择器的顺序没有关系. <!DOCTYPE htm ...

  8. 170925_1 Python socket 创建TCP的服务器端和客户端

    [Python版本]3.6 [遇到的问题] 客户端和服务器端都遇到:TypeError: a bytes-like object is required, not 'str' [解决方案] 参考:ht ...

  9. MSSQL高并发下生成连续不重复的订单号

    一.确定需求 只要做过开发的基本上都有做过订单,只要做过订单的基本上都要涉及生成订单号,可能项目订单号生成规则都不一样,但是大多数规则都是连续增长. 所以假如给你一个这样的需求,在高并发下,以天为单位 ...

  10. groupbox

    使用groupbox将radiobox 放入其中可以使组框中只选中一个