Hdoj1584 spider brand interval DP

Source: Internet
Author: User

Hdoj1584 spider brand interval DP

Question Link

Analysis:
F [I] [j] indicates the minimum number of steps required to stack a deck of cards I to j.
D [I] [j] indicates the number of steps required to move the card I to card j (in the initial status ).
Take a string of cards 3 ~ 8. For example, we need to place card 3 on Card 4. In the optimal mobile solution, the position of Card 4 is unknown, therefore, we can enumerate the position of Card 4 (because there are 10 cards in total, enumeration is acceptable) to obtain the state transition equation: f [3] [8] = min (f [3] [8], f [4] [k] + f [k] [8] + d [3] [k]); (4 <= k <= 8 ). F [I] [j] = min (f [I] [j], f [I + 1] [k] + f [k] [j] + d [I] [k]);

For example, the initial order of a card is 1, 4, 6, 8, 3, 2, 5, 7, 9, 10.
F [1] [4. In the most order: first move Card 2 to card 3, and then move Card 2 ~ 3 move to Card 4. Finally, Move card 1 to Card 2. Now Card 2 is in the position of Card 4. (F [1] [4] = f [2] [4] + f [4] [4] + d [1] [4] = 5 ).

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; int t, a [15], d [15] [15], f [15] [15]; void dp () {for (int I = 0; I <10; I ++) // The obtained range is increasing. First, the interval with a small distance is obtained to obtain the optimal subproblem solution {for (int j = 1; j <= 10; j ++) // the minimum number of steps required to stack a string of cards from j to I + j. {If (I + j> 10) continue; for (int k = j + 1; k <= I + j; k ++) // The Position of the card on the enumeration. f [j] [I + j] = min (f [j] [I + j], f [j + 1] [k] + f [k] [I + j] + d [j] [k]) ;}} void Init () {for (int I = 1; I <= 10; I ++) scanf ("% d", & a [I]); memset (d, 0, sizeof (d); for (int I = 1; I <= 10; I ++) // preprocess all distances to {for (int j = 1; j <= 10; j ++) {int x = a [I], y = a [j]; d [x] [y] = abs (I-j ); d [y] [x] = d [x] [y] ;}} int main () {cin> t; while (t --) {for (int I = 1; I <= 10; I ++) {for (int j = 1; j <= 10; j ++) {f [I] [j] = 10e8; if (I = j) f [I] [j] = 0 ;}} Init (); dp (); printf ("% d \ n", f [1] [10]);} return 0 ;}
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.