Shaped pressure DP [Usaco2008 Nov] mixup2 confusion cows

 

It is limited, at the problem is not the solution, a lot of understanding

 

Thank you for watching this konjac

 

 

topic:

Problem H: [Usaco2008 Nov] mixup2 confusion cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 109  Solved: 59
[Submit][Status][Web Board]

Description

Cows confusion [Don Piele, 2007] Farmer John of N (4 <= N <= 16) in each end cow has a unique number S_i (1 <= S_i <= 25,000). Their number of cows 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 a so any two adjacent ranks cows differ by more than the number of 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
* first 2..N + 1: Line i + 1 contains a row for i represents an integer number of 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 
two methods are: 
. 3. 1. 4 2 
2. 4. 3. 1

HINT

Transfer method similar to Floyd, enumeration transit point f [i] [j] = f [i] [k] + f [k] [j]; just like pressure DP is transferred from one state, then the statistics sum, namely: f [i] [j] = f [i] [j] + f [the state] [K];

Detailed look at ways in which the transfer to another question: https://www.cnblogs.com/nlyzl/p/11281593.html

code:

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int n-, m;
 Long  Long F [ . 1 << . 17 ] [ . 17 ];
 // F [i] [J] [denotes the i-th state position] [ number scheme last cow is j] of 
int S [ 20 is ];
 Long  Long ANS = 0 ;
 int main () 
{ 
    Scanf ( " % D% D " , & n-, & m);
     for ( int I = . 1 ; I < n-=; I ++ ) 
        Scanf ( " % D " , & S [I]);
    for ( int I = . 1 ; I <= n-; I ++ )
     // 10000. 1 ...... 
     // 1000 ....... 1 
     // 100 ....... 1 
     // 10. 1 ...... 
        F [ . 1 << (I- . 1 )] [ I] = . 1 ; //     when taking only a cow, initially. 1; 
    
    for ( int I = . 1 ; I <= ( . 1 << n-) - . 1 ; I ++ )     
          // 1111
          // 10000-1 == 1111 Therefore, for the n-th power of 2 -1 n-bit left shift == 1 -1 
        for ( int J = 1 ; J <= n; J ++)
             IF (I & ( . 1 << (J- . 1 )))
                 for ( int K = . 1 ; K <= n-; K ++ )
                     IF ! (J = K && I & ( . 1 << (- K- . 1 )) && ABS (S [J ] -s [k])> m)
     // based on the meaning of problems: the difference is greater than m, and to ensure that the k-th and j-selected cow is also selected from the cows, and j = k!. 
                        F [I] [J] + = F [I ^ ( . 1 << (J- . 1 ))] [K];
     // a state of the current state of the program number of the program == from accumulated number 
    for ( int I = . 1 ; I <= n-; I ++ ) 
        ANS + F = [( . 1 << n-) - . 1] [I];
 // when full is taken (1111), in any case that a cow can end, again with the ans statistical program and the number of 
    the printf ( " % LLD \ n- " , ans); 
 }

 

Guess you like

Origin www.cnblogs.com/nlyzl/p/11297198.html