洛谷 2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
一道水状压,然而不知道是不是太久没做过dp了,我盯着它二十分钟才反应过来。。。。
还把数组开小了WA了一发QAQ
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
const int maxn=(1<<16)+2;
typedef long long LL;
using namespace std;
int n,kk,nn,cnt[maxn],id[17];
LL dp[2][maxn][16]; namespace fastIO {
const int sz=1<<15|1;
char ch,buf[sz],*l,*r;
void gechar(char &c) {
if(l==r) r=(l=buf)+fread(buf,1,sz,stdin);
c = l==r?(char)EOF:*l++;
}
template<typename T> void read(T &x) {
int f=1; x=0; gechar(ch);
while(ch!='-'&&(ch<'0'||ch>'9')) gechar(ch);
if(ch=='-') f=-1,gechar(ch);
for(;ch>='0'&&ch<='9';gechar(ch)) x=x*10+ch-'0'; x*=f;
}
} void pre() {
nn=(1<<n)-1;
for(int i=0;i<=nn;i++) {
int tp=i;
while(tp) {
cnt[i]++;
tp-=(tp&(-tp));
}
}
} void work() {
int o=0;
dp[o][0][0]=1;
id[0]=1e9;
LL ans=0;
for(int i=1;i<=n;i++) {
o^=1;
for(int j=1;j<=nn;j++) if(cnt[j]==i) {
for(int k=1;k<=n;k++) {
int fl=0;
if(j&(1<<k-1)) {
int pr=j^(1<<k-1);
for(int l=(i!=1);l<=n;l++)
if(dp[o^1][pr][l]&&abs(id[l]-id[k])>kk) {
if(fl==0) {
dp[o][j][k]=dp[o^1][pr][l];
fl=1;
}
else dp[o][j][k]+=dp[o^1][pr][l];
}
}
if(!fl) dp[o][j][k]=0;
if(i==n)
ans+=dp[o][j][k];
}
}
}
printf("%lld\n",ans);
} void init() {
fastIO::read(n);
fastIO::read(kk);
for(int i=1;i<=n;i++) fastIO::read(id[i]);
} //#define DEBUG
int main() {
#ifdef DEBUG
freopen("1.in","r",stdin);
//freopen(".out","w",stdout);
#endif
init();
pre();
work();
return 0;
}
洛谷 2915 [USACO08NOV]奶牛混合起来Mixed Up Cows的更多相关文章
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 状压动归
考场上空间开大了一倍就爆0了QAQ- Code: #include<cstdio> #include<algorithm> #include<cmath> usin ...
- 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】
类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...
- [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
- luogu P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
- [USACO08NOV]奶牛混合起来Mixed Up Cows(状态压缩DP)
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
- 【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
随机推荐
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 (也就是跨数据库访问出错)
delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...
- Southeastern European Regional Programming Contest 2019
easy: I medium-easy: BDEGJ medium: F medium-hard: A A. B. 按 x 排序,\(dp[i][j][k]\) 表示考虑前 \(i\) 个物品,lev ...
- xwiki系统 知识库 xwiki
1.下载tomcat tar -zxvf apache-tomcat-8.0.14.tar.gz mv apache-tomcat-8.0.14 tomcat-xwiki-8.0 2.下载xwik ...
- PHP算法之增减字符串匹配
给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length. 返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i ...
- Algo: maxSubArray vs. maxProduct
这两个问题类似,都可利用动态规划思想求解. 一.最大连续子序列和 https://leetcode.com/problems/maximum-subarray/description/ https:/ ...
- canvas像素的操作
###在canvas中的像素操作 到目前为止,我们尚未深入了解Canvas画布真实像素的原理,事实上, 你可以直接通过ImageData对象操纵像素数据,直接读取或将数据数组写入该对象中 ###得到场 ...
- ERROR 1872
解决 > start slave; ERROR (HY000): Slave failed to initialize relay log info structure from the rep ...
- thinkphp 模块化设计
一个完整的ThinkPHP应用基于模块/控制器/操作设计,并且,如果有需要的话,可以支持多入口文件和多级控制器. ThinkPHP3.2采用模块化的架构设计思想,对目录结构规范做了调整,可以支持多模块 ...
- thinkphp url大小写
系统默认的规范是根据URL里面的模块名.控制器名来定位到具体的控制器类的,从而执行控制器类的操作方法. 以URL访问 http://serverName/index.php/Home/Index/in ...
- 关于maven工程将model删除重建之后变为灰色的问题的解决
问题描述: IDEA中的maven工程中有时候将model或者子model建错,删除之后,子模块在maven在侧栏的maven projects中是灰色的,而且是并没有依赖父工程 在网上搜了搜解决办法 ...