洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
题目描述
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.
输入输出样例
说明
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…的更多相关文章
- 折半搜索+状态压缩【P3067】 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
Description 给n个数,从中任意选出一些数,使这些数能分成和相等的两组. 求有多少种选数的方案. Input 第\(1\)行:一个整数\(N\) 第\(2\)到\(N+1\)行,包含一个整数 ...
- [luogu3067 USACO12OPEN] 平衡的奶牛群
传送门 Solution 折半搜索模板题 考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可 Code //By Menteur_Hxy #include <cmath> #i ...
- 洛谷 P4016负载平衡问题【费用流】题解+AC代码
洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷P3067 平衡的奶牛群 [USACO12OPEN] meet-in-the-middle
正解:搜索 解题报告: 先放下传送门QwQ 这题就,双向搜索经典题鸭 首先dfs应该挺好想到的我jio得?就是我们不用记录左右分别得分多少只要记下差值就好了嘛能get? 然后就先搜左边,记录下每个得分 ...
- 洛谷 [P4016] 负载平衡问题
贪心做法 第一眼看见觉得和均分纸牌差不多,然而因为这是环形的,并不能用均分纸牌的方法做,但是均分纸牌的思想仍然适用 首先我们假设平均数为sum1. 那么对于第1个人,我们假设他给第N个人K个糖果, 第 ...
- 洛谷P4016负载平衡
题目 负载平衡问题是一个比较经典的网络流问题,但是该问题还有一个数学贪心法. 所以做这个题前,其实可以做一下均分纸牌问题. 均分纸牌问题 均分纸牌问题可以说是作为贪心的入门题. 做法 首先我们应当把原 ...
- 洛谷P4104 [HEOI2014]平衡(dp 组合数学)
题意 题目链接 Sol 可以把题目转化为从\([1, 2n + 1]\)中选\(k\)个数,使其和为\((n+1)k\). 再转化一下:把\((n+1)k\)划分为\(k\)个数,满足每个数在范围在\ ...
随机推荐
- php的session
来源:http://blog.163.com/lgh_2002/blog/static/4401752620105246517509/ http协议是WEB服务器与客户 端(浏览器)相互通信的协议,它 ...
- [Swift通天遁地]二、表格表单-(12)设置表单文字对齐方式以及自适应高度的文本区域TextArea
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]八、媒体与动画-(8)使用开源类库快速实现位移动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Akka源码分析-FSM
akka还有一个不常使用.但我觉得比较方便的一个模块,那就是FSM(有限状态机).我们知道了akka中Actor模型的具体实现之后,就会发现,Akka的actor可以非常方便的实现FSM.其实在akk ...
- HttpFileCollection 类使用
public ActionResult GetForm() { HttpRequest request = System.Web.HttpContext.Curre ...
- vue2.0 引入font-awesome
网上的大部分教程复杂而且难看懂,其实两步就能搞定. 先cnpm install font-awesome --save引入依赖 然后在main.js引入 font-awesome/css/font-a ...
- HDU 1754 Java
退役后学java... 裸线段树 //By SiriusRen import java.util.*; import java.math.*; public class Main{ public st ...
- [转]linux uniq 命令详解
转自:http://blog.csdn.net/tianmohust/article/details/6997683 uniq 命令 文字 uniq 是LINUX命令 用途 报告或删除文件中重复的 ...
- 【MySQL】二进制分发安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ...
- js分页插件
//分页插件1function showView(option) { //参数定义id,页容量,当前页,总数,页总数 var id = option.id, pageSiz ...