洛谷 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 & ...
随机推荐
- k8s 对接glusterfs存储
service 与 endpoint 是通过namespace相同,name相同,相互关联的 创建endpoint [root@k8s-master glusterfs]# cat glusterfs ...
- 《DSP using MATLAB》Problem 8.43
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Mybatis 使用的 9 种设计模式,真是太有用了~
Java技术栈 ) { name = fullname.substring(0, delim); children = fullname.substring(delim + 1); ...
- JAVA利用JXL导出 EXCEL (在原有的excel模板上把数据导到excel上)
添加依赖 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>j ...
- c_ 数据结构_图_邻接矩阵
程序主要实现了图的深度遍历和广度遍历. #include <stdio.h> #include <stdlib.h> #include <string.h> #de ...
- GoDaddy商务主机建站具有的优势
GoDaddy是世界第一域名注册服务商,近年来凭借着优异的性能受到国内站长的欢迎,其中Godaddy商务主机得到了很多站长的喜爱,那么为什么GoDaddy商务主机可以受到那么多站长的喜爱呢?下面就带大 ...
- C++ 系列:随机数
C++中没有自带的random函数,要实现随机数的生成就需要使用rand()和srand().不过,由于rand()的内部实现是用线性同余法做的,所以生成的并不是真正的随机数,而是在一定范围内可看为随 ...
- Educational Codeforces Round 27 D. Driving Test
单调栈 题意看了半天... #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...
- 关于SecureCRT不能显示输入、换行不正常
网上绿色破解版的SecureCRT会碰到这种问题,即输入字符不显示在终端.换行后下一行的行首有很大一段退格: 如上,输入的指令不显示:回车后下一行的起始位置不对. 碰到这种问题,在波特率.奇偶校验.停 ...
- 【JZOJ6367】工厂(factory)
description 大神 wyp 开了家工厂,工厂有 n 个工人和 p 条流水线. 工厂的工人都是睡神,因此第 i 个工人只会在 si 至 ti 时刻才会工作. 每个工人都会被分派到一条流水线上, ...