zoukankan      html  css  js  c++  java
  • 最长上升子序列

    #include<iostream>
    using namespace std;
    #define MAX_N 100
    int N;
    int b[MAX_N + 10];
    int aMaxLen[MAX_N + 10];
    int r[MAX_N + 10];//记录路径
    /*
    7
    1 7 3 5 9 4 8

    4
    */

    //递归打印
    void print_path(int index)
    {
         if(aMaxLen[index]>1)
         {
             print_path(r[index]);
             printf("%d ", b[index]);   
         }
         else
         {
             printf("%d ", b[index]);
         }
        
                    
    }

    int main()
    {
         int m;
         scanf("%d", &N);
         for( int i = 1; i <= N; i ++ )
             scanf("%d", &b[i]);
        
         aMaxLen[1]=1;
         for(int i=2;i<=N;i++)
         {
             int tmp=0;
             for(int j=1;j<=i-1;j++)
                 if(b[i]>b[j])
                 {
                     if(aMaxLen[j]>tmp)
                     {
                         tmp=aMaxLen[j];
                         r[i]=j;
                     }
                 }
             aMaxLen[i]=tmp+1;
         }

        int nMax=0;
         int index=0;
         for(int i=1;i<=N;i++)
         {
             if(aMaxLen[i]>nMax)
             {
                 nMax=aMaxLen[i];
                 index=i;
             }
                
         }
         printf("%d ", nMax);
         printf("%d ", index);
         //print_path
         for(int i=1;i<=N;i++)
         {
             printf("%d ", r[i]);
         }
         printf(" ");
         print_path(index);

        return 0;
    }


    http://poj.org/problem?id=3903

  • 相关阅读:
    Python Twelfth Day
    Python Tenth Day
    Python Ninth Day
    Python Eighth Day
    Python Seventh Day
    Python Sixth Day
    Python Fifth Day
    Python Fourth Day
    Python Third Day
    金融量化分析-python量化分析系列之---使用python的tushare包获取股票历史数据和实时分笔数据
  • 原文地址:https://www.cnblogs.com/cute/p/15266581.html
Copyright © 2011-2022 走看看