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

  • 相关阅读:
    Shell 基本运算符
    如何将mongo查询结果导出到文件中
    python如何将自己写的代码打包供他人使用
    json格式化
    如何在linux安装ruby2.2.2+
    如何对字符串进行码表替换转码--加密encode
    git基础
    如何创建redis集群
    linux安装python2.7
    如何在windows和linux搭建django环境
  • 原文地址:https://www.cnblogs.com/cute/p/15266581.html
Copyright © 2011-2022 走看看