P2915 [USACO08NOV]奶牛混合起来(状压dp入门)

来源:互联网 发布:java多线程基础 编辑:程序博客网 时间:2024/05/23 19:32

约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的。这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍。在一只混乱的队 伍中,相邻奶牛的编号之差均超过K。比如当K = 1时,1, 3, 5, 2, 6, 4就是一支混乱的队伍, 而1, 3, 6, 5, 2, 4不是,因为6和5只差1。请数一数,有多少种队形是混乱的呢?

输入输出样例

输入样例#1:
4 1
3
4
2
1
输出样例#1:
2

program df;
var i,j,n,m,x,y,z,k:longint;
t:int64;
a,b:array[0..1000000] of longint;
f:array[0..16,0..1 shl 16] of int64;
begin
readln(n,m);
for i:=0 to n-1 do
readln(a[i]);

for i:=0 to n-1 do
f[i,1 shl i]:=1;

for j:=0 to (1 shl n)-1 do
for i:=0 to n-1 do

if (1 shl i and j)=1 shl i then
for k:=0 to n-1 do
if ((j and (1 shl k))=0) and (abs(a[i]-a[k])>m) then
f[k,j or (1 shl k)]:=f[k,j or (1 shl k)]+f[i,j];

t:=0;
for i:=0 to n-1 do
t:=t+f[i,(1 shl n)-1];
writeln(t);
end.

原创粉丝点击