LianLianKan HDU - 4272-like pressure dp

The meaning of problems: a length of n (n <= 1000) of the stack, the top element can be eliminated in the same number of elements in the following 1 to 5, Q can finally completely eliminated.

 

answer:

This sequence example
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
11
farthest position 2 in this position can be matched to 11
Why?
Because this position can be matched 1 to 6, 3, 4, that is to say these locations are likely to be above match too (I wrote sequence certainly not enough, it will)
then the rest of the sequence becomes became
2
. 7
. 8
. 9
10
11
so that the furthest distance 11 matching the 2
So we have to save the state at least nine positions, because we only need to make sure whether the number of the position offset chant over
so we can use the number 1 for this position has been offset, with 0 being no offset
and then put the nine states as the binary form, and then compressed into a digital int size
Therefore, the beginning state is dp [0] [0] = 1
if dp [i] [j] value represents i-1 from the top of the stack number that is offset position is offset represents 1, 0 representing no offset
dp [0] [0] is set only to run the following program
Because first of all to ensure that the title element to offset the top of the stack, so each of us to dp [i] [] time of assignment must make sure, dp [i-1] [ ] value of a position is a
specific look at the code
 
 
Code:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 typedef long long ll;
 6 using namespace std;
 7 const int maxn=1005;
 8 const int M=1<<9;
 9 int v[maxn],dp[maxn][1<<9];
10 int main()
11 {
12     int n;
13     while(~scanf("%d",&n))
14     {
15         memset(dp,0,sizeof(dp));
16         for(int i=n;i>0;--i)
17             scanf("%d",&v[i]);
18         dp[0][0]=1;
19         for(int i=1;i<=n;++i)
20         {
21             for( Int J = 0 ; J <M; ++ J)
 22 is              {
 23 is                  IF (DP [I- . 1 ] [J]) // because the topics described first offset stack of elements, so each must find the time to find a position element is offset 
24                  {
 25                      IF (& J . 1 ) // this determination is used to determine the position number is not being eliminate it on a location 
26 is                      {
 27                          DP [I] [J >> . 1 ] = 1 ;   // since this compressed state j to a state 10 positions below it, so that on the end j >> 1 
28                      }
 29                      the else 
30                      {
 31 is                          int= T 0 ; // record the position of the several positions actually sinking 
32                          for ( int K = . 1 ; K <= . 8 ; ++ K)
 33 is                          {
 34 is                              IF ! ((& J ( . 1 << K)) && KT <= . 5 && V [I] == V [I + K])
 35                              {
 36                                  DP [I] [(J >> . 1 ) | ( . 1 << (- K- . 1 ))] = . 1 ;
 37 [                              }
 38 is                              the else  IF (J & ( 1 <<k))
39                                 t++;
40                         }
41                     }
42                 }
43             }
44         }
45         if(dp[n][0]==1)
46             printf("1\n");
47         else printf("0\n");
48     }
49     return 0;
50 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12082026.html