Topcodersrm653div. 21000

Source: Internet
Author: User
Tags bitset
A sequence is provided. Please find two subsequences that are not handed over and are complete, so that the absolute sum of the Number Difference between two adjacent sub-sequences is the most

A sequence is provided. Please find two subsequences that are not handed over and are complete, so that the absolute sum of the Number Difference between two adjacent sub-sequences is the most

Question:

Given a sequence, please find two subsequences that are not handed over and are complete sets, so that the sum of the absolute values of the Number Difference between two adjacent sub-sequences is the smallest.

Ideas:

DP ..

Dp [I] [j] indicates that the last position of a person is I, and the last position of a person is j.

There are two situations:

1. i-j> 1: dp [I] [j] = dp [I-1] [j] + abs (pitch [I-2]-pitch [I-1]) keep A number ..

2. I-j = 1: 1) A takes only I, and all others are given to B.

2) A takes j and enumerates the position k obtained last time by A, B Takes I, and the last time k: res = min (res, dp [j] [k] + abs (I-1]-pitch [k-1])

Understanding: two people will regard one of the positions as the last position, and the last position of the other person is the last number. you can think of three people first. In this way, all the situations can be covered, which is equivalent to the subproblem of the problem.

AC.

    #line 7 "SingingEasy.cpp"    #include 
 
      #include 
  
       #include 
   
        #include 
    
         #include 
     
          #include     #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #include 
                     
                       #include 
                      
                        using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(i=0;i<(n);++i) #define FOR(i,l,h) for(i=(l);i<=(h);++i) #define FORD(i,h,l) for(i=(h);i>=(l);--i) typedef vector
                       
                         VI; typedef vector
                        
                          VS; typedef vector
                         
                           VD; typedef long long LL; typedef pair
                          
                            PII; int dp[2005][2005]; class SingingEasy { public: int solve(vector 
                           
                             pitch) { int len = pitch.size(); if(len <= 2) return 0; dp[1][0] = 0; for(int j = 2; j <= len; ++j) { dp[j][0] = dp[j-1][0] + abs(pitch[j-2]-pitch[j-1]); } dp[2][1] = 0; for(int i = 3; i <= len; ++i) { for(int j = 1; j < i; ++j) { if(i - j > 1) { dp[i][j] = dp[i-1][j] + abs(pitch[i-2]-pitch[i-1]); } else { int res = 1e9+5; for(int k = 1; k < j; ++k) { res = min(res, dp[j][k] + abs(pitch[i-1]-pitch[k-1])); } dp[i][j] = min(res, dp[j][0]); } } } int ans = dp[len][0]; for(int i = 1; i < len; ++i) { ans = min(ans, dp[len][i]); } return ans; }
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
         
        
       
      
     
    
   
  
 
Related Article

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.