[luogu3067 USACO12OPEN] 平衡的奶牛群
Solution
折半搜索模板题
考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可
Code
//By Menteur_Hxy
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Re register
#define Ms(a,b) memset(a,(b),sizeof(a))
#define Fo(i,a,b) for(Re int i=(a),_=(b);i<=_;i++)
#define Ro(i,a,b) for(Re int i=(b),_=(a);i>=_;i--)
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<16,stdin)),p1==p2?EOF:*p1++)
using namespace std;
typedef long long LL;
char buf[1<<16],*p1,*p2;
inline int read() {
int x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
inline void writ(int x) {
if(x>9) writ(x/10);
putchar(x%10+'0');
}
const int N=25,M=1e8+5;
int n,ans,tt1,tt2;
bool vis[1<<N];
int da[N];
struct Data{int val,cur;}T1[1<<N],T2[1<<N];
void dfs(int pos,int ed,int sum,int S) {
if(pos>ed) {
if(ed==n/2) T1[++tt1].val=sum,T1[tt1].cur=S;
else T2[++tt2].val=sum,T2[tt2].cur=S;//1
return ;
}
dfs(pos+1,ed,sum,S);
dfs(pos+1,ed,sum+da[pos],S|(1<<(pos-1)));
dfs(pos+1,ed,sum-da[pos],S|(1<<(pos-1)));
}
bool cmp1(Data a,Data b) {return a.val<b.val;}
bool cmp2(Data a,Data b) {return a.val>b.val;}
int main() {
n=read();
Fo(i,1,n) da[i]=read();
dfs(1,n/2,0,0); dfs(n/2+1,n,0,0);
sort(T1+1,T1+1+tt1,cmp1);
sort(T2+1,T2+1+tt2,cmp2);
int l=1,r=1,las;
while(l<=tt1&&r<=tt2) {
while(r<=tt2&&T1[l].val+T2[r].val>0) r++;//2
las=r;
while(r<=tt2&&T1[l].val+T2[r].val==0) {
if(!vis[T1[l].cur|T2[r].cur])
vis[T1[l].cur|T2[r].cur]++,ans++;
r++;
}
l++;
if(T1[l-1].val==T1[l].val) r=las;//3
}
writ(ans-1);
return 0;
}
[luogu3067 USACO12OPEN] 平衡的奶牛群的更多相关文章
- 洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S… 题目描述 Farmer John's owns N cows (2 <= N <= 20), where ...
- 折半搜索+状态压缩【P3067】 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
Description 给n个数,从中任意选出一些数,使这些数能分成和相等的两组. 求有多少种选数的方案. Input 第\(1\)行:一个整数\(N\) 第\(2\)到\(N+1\)行,包含一个整数 ...
- Luogu3067 平衡的奶牛群 Meet in the middle
题意:给出$N$个范围在$[1,10^8]$内的整数,问有多少种取数方案使得取出来的数能够分成两个和相等的集合.$N \leq 20$ 发现爆搜是$O(3^N)$的,所以考虑双向搜索. 先把前$3^\ ...
- P3067 [USACO12OPEN]平衡的奶牛群(折半暴搜)
暴搜无疑.... 首先考虑纯暴搜...... 考虑每一个数: 选在左边集合 选在右边集合 不选 一共三种情况,用一个数组记录搜到的答案,所以暴搜是3^N的复杂度...直接死亡 于是讲折半暴搜.... ...
- 洛谷P1472 奶牛家谱 Cow Pedigrees
P1472 奶牛家谱 Cow Pedigrees 102通过 193提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 农民约翰准备 ...
- 【USACO 2.3.2】奶牛家谱
[题目描述] 农民约翰准备购买一群新奶牛.在这个新的奶牛群中,每一个母亲奶牛都生两小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些二叉 ...
- Meet in the middle
搜索是\(OI\)中一个十分基础也十分重要的部分,近年来搜索题目越来越少,逐渐淡出人们的视野.但一些对搜索的优化,例如\(A\)*,迭代加深依旧会不时出现.本文讨论另一种搜索--折半搜索\((meet ...
- 2017清北学堂(提高组精英班)集训笔记——动态规划Part3
现在是晚上十二点半,好累(无奈脸),接着给各位——也是给自己,更新笔记吧~ 序列型状态划分: 经典例题:乘积最大(Luogu 1018) * 设有一个长度为 N 的数字串,要求选手使用 K 个乘号将它 ...
- 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
随机推荐
- HTTP要点概述:六,HTTP报文
一,HTTP报文: 用于HTTP交互的信息称为HTTP报文.请求端(客户端)的HTTP报文叫做请求报文,响应端(服务器)的叫做响应报文.HTTP报文本身是由多行(用CR+LF换行)数据构成的字符串文本 ...
- Struts数据验证
Action类继承了ActionSupport类,而该类实现了Action.Validateable.ValidationAware.TextProvider.LocaleProvider和Seria ...
- POJ1808 平方(二次)同余方程
POJ1808 给定一个方程 x*x==a(mod p) 其中p为质数 判断是否有解 程序中 MOD_sqr()返回整数解 无解返回-1 数学证明略 #include<iostream> ...
- 洛谷 P1966 火柴排队 —— 思路
题目:https://www.luogu.org/problemnew/show/P1966 首先,一个排列相邻交换变成另一个排列的交换次数就是逆序对数: 随便画一画,感觉应该是排个序,大的对应大的, ...
- MySQL-ProxySQL中间件(二)| Admin Schemas介绍
目录 MySQL-ProxySQL中间件(一)| ProxySQL基本概念: https://www.cnblogs.com/SQLServer2012/p/10972593.html ...
- 模拟 HDOJ 4552 Running Rabbits
题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...
- 257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ...
- 安装Kali里的应用程序或软件包
安装Kali里的应用程序或软件包 安装额外的软件是apt-get命令最基本的功能了,命令非常简单易懂.安装软件包的语法如下: apt-get install 软件包名 比如,安装图像编辑软件gimp, ...
- [转]TFS下的源代码控制
本文转自:http://www.cnblogs.com/ajiefj/archive/2010/04/23/1718450.html 以下主要描述了: TFS源代码控制系统的基本场景 如何把一个项目添 ...
- LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...