BZOJ1231: [Usaco2008 Nov] mixup2 confusion cows (like pressure DP)

Description
confusion cows [Don Piele, 2007] Farmer John of N (4 <= N <= 16) cows in each head has a unique number S_i (1 <= S_i <= 25,000). Cows for their number proud, so each cow regarded her number engraved on a gold medal and the gold medal hanging in their wide neck. cows to be arranged in a "chaos" at the time of milking very offensive team If any two adjacent ranks of a number of cows differ by more than K (1 <= K <= 3400), it is known to be chaotic. For example, when N = 6, K = 1, 1, 3, 5, 2, 6, 4 is a "chaos" of the team, but the 1, 3, 6, 5, 2, 4 is not (because of a difference of 5 and 6 1). so, how many cows can row into "chaos" of the team's options?

Input

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

  • Of 2 ... N + 1: Line i + 1 row contains an integer number used to represent the i-th cows: S_i

Output
Line 1: Only one integer that represents how many cows can be arranged in the "chaos" of the team to ensure that the program is the answer to a 64-bit integer in the range.

Sample Input
4 1

3

4

2

1

Sample Output
2

Output Interpreter:

Both methods are:

3 1 4 2

2 4 1 3

Thinking:
Representative F [i] [j] is state i, j is the end state. And then next to the last enumeration insertion point k.
Transfer is F [i | (1 << k )] [k] + = F [i] [j] (k - j)> K.

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

ll s[1005],f[16][1 << 16],vis[200005];

int main()
{
    int n,K;scanf("%d%d",&n,&K);
    for(int i = 0;i < n;i++)
    {
        scanf("%lld",&s[i]);
    }
    for(int i = 0;i < n;i++){f[i][1 << i] = 1;}
    
    for(int i = 0;i < (1 << n);i++)
    {
        for(int j = 0;j < n;j++)
        {
            if(i & (1 << j))
            {
                for(int k = 0;k < n;k++)
                {
                    if(!(i & (1 << k)) && abs(s[j] - s[k]) > K)
                    {
                        f[k][i | (1 << k)] += f[j][i];
                    }
                }
            }
        }
    }
    
    ll ans = 0;
    for(int i = 0;i < n;i++)ans += f[i][(1 << n) - 1];
    
    printf("%lld\n",ans);
    return 0;
}

Published 676 original articles · won praise 18 · views 30000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104207334