一道水状压,然而不知道是不是太久没做过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的更多相关文章

  1. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...

  2. 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

  3. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

  4. 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 状压动归

    考场上空间开大了一倍就爆0了QAQ- Code: #include<cstdio> #include<algorithm> #include<cmath> usin ...

  5. 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】

    类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...

  6. [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  7. 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 & ...

  8. [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 & ...

  9. 【题解】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 & ...

随机推荐

  1. LightOJ-1282-Leading and Trailing-快速幂+数学

    You are given two integers: n and k, your task is to find the most significant three digits, and lea ...

  2. JAVA发展历史!

    前言 自1946年2月14日世界上首款计算机问世,第一代计算机语言“机器语言”便诞生了,它使用的是最原始的穿孔卡片,这种卡片上使用的语言只有专家才能理解,与人类语言差别极大.这种语言本质上是计算机能识 ...

  3. scrapy的使用--Rcrapy-Redis

    Scrapy-Redis分布式爬虫组件 Scrapy是一个框架,他本身是不支持分布式的.如果我们想要做分布式的爬虫.就需要借助一个组件叫做Scrapy-Redis.这个组件正式利用了Redis可以分布 ...

  4. 使用CSS为图片添加更多趣味的5种方法

    使用Photoshop为每个图片添加某种样式虽然可行,但会是相当乏味且困难的长久工作.下面要介绍的CSS技巧将帮助你从痛苦中解脱出来! 阴影效果 通过使用带有一些padding之的背景图来添加阴影效果 ...

  5. Error: setup script specifies an absolute path

    在安装sklearn的时候,出现: error: Error: setup script specifies an absolute path: /opt/xgboost-0.47/python-pa ...

  6. Swaks伪造邮件

    一.搭建邮件服务器 首先需要自己搭建邮件服务器采用的是EwoMail搭建参考链接: http://doc.ewomail.com/docs/ewomail/install 二.邮件伪造发送 swaks ...

  7. 多版本JDK 切换

    由于一些原因,我本机存在3个版本的jdk. 但是发现,单纯去 修改环境变量,并没有效果. 那么我们下面看看怎么改 1 查看本机版本    java -version 2 查看jdk路径   where ...

  8. python日常使用

    os.path.splitext('C:\py\wxPython.gif')  得到扩展名的函数 os.remove(删除文件) os.listdir(显示该目录下的文件) os.getcwd(获取当 ...

  9. Scala 方法与函数简单记录

    /** * Scala 方法与函数 * Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法 */ o ...

  10. day20 装饰器补充

    Python之路,Day8 = Python基础8 装饰器from functools imoort wraps # 保留原函数所有信息,比如:用__doc__查看注释的时候,显示原来的注释def f ...