【Question】Luogu2915 [USACO08NOV] Mixed Up Cows

Topic description

Each of Farmer John’s N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her ample bovine neck.

Gangsta cows are rebellious and line up to be milked in an order called ‘Mixed Up’. A cow order is ‘Mixed Up’ if the sequence of serial numbers formed by their milking line is such that the serial numbers of every pair of consecutive cows in line differs by more than K (1 <= K <= 3400). For example, if N = 6 and K = 1 then 1, 3, 5, 2, 6, 4 is a ‘Mixed Up’ lineup but 1, 3, 6, 5, 2, 4 is not (since the consecutive numbers 5 and 6 differ by 1).

How many different ways can N cows be Mixed Up?

For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.

POINTS: 200

John's family has N cows, the number of the ith cow is Si, and the number of each cow is unique. These cows have been throwing tantrums lately, and to express their dissatisfaction, they must form a chaotic line during milking. In a chaotic team, the difference between the numbers of adjacent cows is more than K. For example, when K = 1, 1, 3, 5, 2, 6, 4 are a chaotic team, but 1, 3, 6, 5, 2, 4 are not, because 6 and 5 are only 1 difference. Count how many formations are chaotic?

Input and output format

Input format:

  • Line 1: Two space-separated integers: N and K

  • Lines 2..N+1: Line i+1 contains a single integer that is the serial number of cow i: S_i

Output format:

  • Line 1: A single integer that is the number of ways that N cows can be ‘Mixed Up’. The answer is guaranteed to fit in a 64 bit integer.

Input and output example

Input Example #1: Copy

4 1 
3 
4 
2 
1

Sample Output #1: Copy

2

illustrate

The 2 possible Mixed Up arrangements are:

3 1 4 2

2 4 1 3

ideas

  • Represent the state of the cow in binary
  • Let $f[state][last]$ represent the legal queue number when the cow's arrangement state is state and the last cow is last
    • If and only if the ith cow is not included in the state j, and abs(s[i]-s[last]) > k, the ith cow can be added to the queue
    • 此时可以有转移方程$dp[State∣(1<<i)][i]+=dp[State][last]$

注意 边界条件为 $dp[i][1<<i]=1dp[i][1<<i]=1dp[i][1<<i]=1$

  • 最终答案
    $$answer= \sum_{i=0}^{n-1}dp[(1<<n)-1][i]$$

代码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324729767&siteId=291194637