看到n<=16不难想到状压dp
我们用二进制表示前x个位置,哪些牛被已经被选过了
这里我们可以通过穷举二进制数的顺序来转移
所以二维就够了
1 var a:array[0..20] of longint; 2 f:array[0..1 shl 16,0..20] of int64; 3 n,i,j,k,m,t:longint; 4 ans:int64; 5 6 7 begin 8 readln(n,t); 9 for i:=0 to n-1 do 10 readln(a[i]); 11 m:=1 shl n-1; 12 for i:=0 to n-1 do 13 f[1 shl i,i]:=1; 14 for i:=0 to m do 15 for j:=0 to n-1 do 16 if (i and (1 shl j)<>0) then 17 begin 18 for k:=0 to n-1 do 19 if (i and (1 shl k)=0) and (abs(a[j]-a[k])>t) then 20 inc(f[i or (1 shl k),k],f[i,j]); 21 end; 22 23 ans:=0; 24 for i:=0 to n-1 do 25 ans:=ans+f[m,i]; 26 writeln(ans); 27 end.