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

  • 相关阅读:
    HDU4474 Yet Another Multiple Problem BFS搜索
    HDU4473 Exam 数学分析
    2013ACM多校联合(4)
    POJ1273 网络流...
    HDU4472 Count 递推
    POJ1149 PIGS 网络流
    UVA10881 Piotr's Ants 想法题
    javascript js string.Format()收集
    修改 设置 vs.net 网站 调试 设为 起始页
    【转】HTML5杂谈 概念与现行游戏 割绳子 宝石迷阵
  • 原文地址:https://www.cnblogs.com/cute/p/15266581.html
Copyright © 2011-2022 走看看