洛谷 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\)个数,满足每个数在范围在\ ...
随机推荐
- vue中时间控件绑定多个输入框
首先去下载laydate时间控件,引入到相应的模板中 <input type="text" val-required="" value="&qu ...
- bzoj4563: [Haoi2016]放棋子(错排+高精)
4563: [Haoi2016]放棋子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 387 Solved: 247[Submit][Status] ...
- PID204特种部队
特种部队 题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达 ...
- Unity项目 - 吃豆人Pacman
项目展示 Github项目地址:Pacman 涉及知识 切片制作 Animations 状态机设置,any state切换,重写状态机 按键读取进行整数距离的刚体移动 用射线检测碰撞性 渲染顺序问题 ...
- C#使用Parallel处理数据同步写入Datatable并使用BulkInsert批量导入数据库
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL). 业务流程:选择照片文件夹,分别访问照片-->调用DLL接口传递照片路径-->接收处理返回值-->写入数据 ...
- Jquery 表单基础元素操作总结
最近做前端比较多总结一些常用功能: radio 单选选中并且出发change事件: $(selector).find('input:radio[name=valuationMode]').filter ...
- 八皇后问题---详解---参考<<紫书>>
在一个8*8的棋盘上 放置八个皇后 , 使得他们互相不攻击(皇后攻击范围为 同行同列同对角线) , 方法一 : 从64个格子中 选一个子集 , 使得 " 子集 中恰好有八个元素 , 且任意 ...
- BZOJ 4867 分块+神tm卡常
思路: 注意到len<=10 按照权值max-min<=sqrt(n)*len 分块 记一下前缀和 每修改sqrt(n)次以后重新分块 修改的时候整块打标记 两边重构 (这题常数卡 ...
- Android json 数据解析
1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...
- Oracle 递归的写法(start with) 以及where条件作用域
先转一个讲Oracle递归讲得非常透彻的文章: http://blog.csdn.net/weiwenhp/article/details/8218091 前言:嗯,这也是一个前人挖坑,后人来填的故事 ...