Longest ascending subsequence LIS set POJ2533, POJ1631, POJ1887, POJ1609

Source: Internet
Author: User

POJ2533 finds the longest ascending subsequence naked, and the complexity is the template of n ^ 2.


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Include
          
            # Include
           
             # Include
            
              # Define ll long # define eps 1e-7 # define inf 0 xfffffff // const ll INF = 1ll <61; using namespace std; // vector
             
               > G; // typedef pair
              
                P; // vector
               
                 >:: Iterator iter; // map
                
                  Mp; // map
                 
                   : Iterator p; // # define IN freopen ("c: \ Users \ linzuojun \ desktop \ input.txt", "r", stdin) // # define OUT freopen ("c: \ Users \ linzuojun \ desktop \ output.txt", "w", stdout) int dp [10000 + 5]; int num [10000 + 5]; void clear () {memset (dp, 0, sizeof (dp); memset (num, 0, sizeof (num ));} int main () {int n; while (cin> n) {memset (dp, 0, sizeof (dp); for (int I = 1; I <= n; I ++) cin> num [I]; int ans = 1; for (int I = 1; I <= n; I ++) dp [I] = 1; for (int I = 2; I <= n; I ++) {int m = 0; for (int j = 1; j
                  
                    Num [j] & dp [j] + 1> dp [I]) {dp [I] = dp [j] + 1 ;} if (ans <dp [I]) ans = dp [I] ;}} cout <
                   

POJ1631 is also a naked LIS. The complexity is the nlogn template, and n ^ 2 times out.



# Include
                    
                     
# Include
                     
                      
# Include
                      
                       
# Include
                       
                        
# Include
                        
                         
# Include
                         
                           # Include
                          
                            # Include
                           # Include
                            
                              # Include
                             
                               # Include
                              
                                # Include
                               
                                 # Define ll long # define eps 1e-7 # define inf 0 xfffffff // const ll INF = 1ll <61; using namespace std; // vector
                                
                                  > G; // typedef pair
                                 
                                   P; // vector
                                  
                                    >:: Iterator iter; // map
                                   
                                     Mp; // map
                                    
                                      : Iterator p; // # define IN freopen ("c: \ Users \ linzuojun \ desktop \ input.txt", "r", stdin) // # define OUT freopen ("c: \ Users \ linzuojun \ desktop \ output.txt", "w", stdout) int dp [100000 + 5]; int num [100000 + 5]; void clear () {memset (dp, 0, sizeof (dp); memset (num, 0, sizeof (num ));} int main () {int t, n; cin> t; while (t --) {clear (); cin> n; for (int I = 1; I <= n; I ++) cin> num [I]; int ans = 0; for (int I = 1; I <= n; I ++) {int tmp = num [I]; int left = 1; int right = ans; while (left <= right) {int mid = (left + right)/2; if (dp [mid] <tmp) left = mid + 1; elseright = mid-1;} dp [left] = tmp; if (ans <left) ans ++ ;} cout <
                                     

POJ1887: the maximum number of sub-sequences that do not rise is enough. In this case, pay attention to the output format. There is a leading space, and then there is a blank line between the two cases. It does not remind PE to say WA only.


#include
                                      
                                       #include
                                       
                                        #include
                                        
                                         #include#include
                                         
                                          #include
                                          
                                           #include
                                           
                                            #include
                                            
                                             #include
                                             #include
                                              
                                               #include
                                               
                                                #include
                                                
                                                 #include
                                                 
                                                  #define ll long long#define eps 1e-7#define inf 0xfffffff//const ll INF = 1ll<<61;using namespace std;//vector
                                                  
                                                    > G;//typedef pair
                                                   
                                                     P;//vector
                                                    
                                                      > ::iterator iter;////map
                                                     
                                                      mp;//map
                                                      
                                                       ::iterator p;//#define IN freopen("c:\\Users\\linzuojun\\desktop\\input.txt", "r", stdin) //#define OUT freopen("c:\\Users\\linzuojun\\desktop\\output.txt", "w", stdout) int dp[100000 + 5];int num[100000 + 5];void clear() {memset(dp,0,sizeof(dp));/*memset(num,0,sizeof(num));*/}int main() {int a;int Case = 0;clear();while(scanf("%d",&a)) {clear();int n = 1;if(a == -1)break;num[n] = a;while(true) {scanf("%d",&a);if(a == -1)break;num[++n] = a;}for(int i=1;i<=n;i++)dp[i] = 1;int ans = 1;for(int i=n-1;i>=1;i--) {for(int j=i+1;j<=n;j++) {if(dp[j] + 1 > dp[i] && num[i] > num[j])dp[i] = dp[j] + 1;if(dp[i] > ans)ans = dp[i];}}printf("Test #%d:\n",++Case);printf(" maximum possible interceptions: %d\n\n",ans);}return EXIT_SUCCESS;}
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                            
                                           
                                          
                                         
                                        
                                       
                                      


POJ1609, the longest sub-sequence is not dropped, but there are two restrictions. First, the sorting satisfies one of them, and then carry out the LIS Algorithm on this basis. Note that the non-equal sign must be changed, there are also the most questions, but the sequence is not given, so you need to sort first



#include
                                      
                                       #include
                                       
                                        #include
                                        
                                         #include#include
                                         
                                          #include
                                          
                                           #include
                                           
                                            #include
                                            
                                             #include
                                             #include
                                              
                                               #include
                                               
                                                #include
                                                
                                                 #include
                                                 
                                                  #define ll long long#define eps 1e-7#define inf 0xfffffff//const ll INF = 1ll<<61;using namespace std;//vector
                                                  
                                                    > G;//typedef pair
                                                   
                                                     P;//vector
                                                    
                                                      > ::iterator iter;////map
                                                     
                                                      mp;//map
                                                      
                                                       ::iterator p;//#define IN freopen("c:\\Users\\linzuojun\\desktop\\input.txt", "r", stdin) //#define OUT freopen("c:\\Users\\linzuojun\\desktop\\output.txt", "w", stdout) int dp[100000 + 5];struct Node {int x,y;}node[100000 + 5];void clear() {memset(dp,0,sizeof(dp));memset(node,0,sizeof(node));}bool cmp(Node x,Node y) {if(x.x == y.x)return x.y < y.y;return x.x 
                                                       
                                                         dp[i] && node[i].x >= node[j].x && node[i].y >= node[j].y)dp[i] = dp[j] + 1;if(dp[i] > ans)ans = dp[i];}}printf("%d\n",ans);}return EXIT_SUCCESS;}
                                                       
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                            
                                           
                                          
                                         
                                        
                                       
                                      


Another way is to do this, because the range is relatively small, so you can do it directly by using dp.


#include
                                      
                                       #include
                                       
                                        #include
                                        
                                         #include#include
                                         
                                          #include
                                          
                                           #include
                                           
                                            #include
                                            
                                             #include
                                             #include
                                              
                                               #include
                                               
                                                #include
                                                
                                                 #include
                                                 
                                                  #define ll long long#define eps 1e-7#define inf 0xfffffff//const ll INF = 1ll<<61;using namespace std;//vector
                                                  
                                                    > G;//typedef pair
                                                   
                                                     P;//vector
                                                    
                                                      > ::iterator iter;////map
                                                     
                                                      mp;//map
                                                      
                                                       ::iterator p;//#define IN freopen("c:\\Users\\linzuojun\\desktop\\input.txt", "r", stdin) //#define OUT freopen("c:\\Users\\linzuojun\\desktop\\output.txt", "w", stdout) int num[112][112],dp[112][112];void clear() {memset(num,0,sizeof(num));memset(dp,0,sizeof(dp));}int main() {int n;while(scanf("%d",&n),n) {clear();int x,y;for(int i=1;i<=n;i++) {scanf("%d %d",&x,&y); num[x][y]++;}for(int i=1;i<=100;i++) for(int j=1;j<=100;j++) dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + num[i][j];printf("%d\n",dp[100][100]);}puts("*");return EXIT_SUCCESS;}
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                            
                                           
                                          
                                         
                                        
                                       
                                      




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.