洛谷 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\)个数,满足每个数在范围在\ ...
随机推荐
- Linux基础命令第二波
第1章 Linux启动过程 开机自检(BIOS)##硬件检查 MBR引导 GRUB菜单(选择不同的内核) 加载内核 运行init进程(Linux系统里面第一个进程) 读取/etc/inittab配置文 ...
- codevs1005生日礼物(dfs)
1005 生日礼物 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 9月12日是小松的朋友小寒的生日.小松知道小寒特别 ...
- 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...
- Unity学习-地形的设置(五)
添加地形游戏对象 [Hierarchy-Create-Terrain] 为了看的看清楚,在添加一个平行光 [Hierarchy-Create-Direction light] 导入地形包 [Asset ...
- 11.Layers, Containers and Interfaces
图层.容器和接口 当设计一个Ventuz场景时,某些节点的组合或设计会反复出现.例如在演示中使用的按钮或滑块,在整个过程中的呈现和外观都是一致的,唯一变化的是尺寸.位置和标签. 在设计复杂的演示时,另 ...
- Linux文件系统inode、block解释权限(三)
利用文件系统的inode和block来分析文件(目录)的权限问题. 为什么读取一个文件还要看该文件路径所有目录的权限? 为什么目录的w权限具有删除文件的能力,而文件w权限不行. inode:记录文件的 ...
- xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理
异常如下: org.dom4j.DocumentException: Error on line 2 of document file:///D:/workspaces/struts2/lesson0 ...
- CSS——样式隐藏
overflow:hidden: 溢出隐藏 visibility:hidden: 隐藏元素 隐藏之后还占据原来的位置. display:none: 隐藏元素 隐藏之后不占据 ...
- CSS——新浪导航demo
主要运用的dispaly将a变成行内块,再用padding撑开宽度. <!DOCTYPE html> <html lang="en"> <head&g ...
- VHDL之Serial-Parallel Multiplier
1 Serial-parallel multiplier Figure 12.1 shows the RTL diagram of a serial-parallel multiplier. One ...