zoukankan      html  css  js  c++  java
  • 求最长不下降子序列

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int maxx=1001;
     5 int n,a[maxx][5];//a[x][1]记录内容,a[x][2]记录到目前的最长不下降子序列长度,a[x][3]记录前驱 
     6 int main()
     7 {
     8     scanf("%d",&n);
     9     for(int i=1;i<=n;i++)
    10     {
    11         scanf("%d",&a[i][1]);
    12         a[i][2]=1;
    13         a[i][3]=0;
    14     }
    15     for(int i=n-1;i>=1;i--)//从后往前找 
    16     {
    17         int l=0,k=0;
    18         for(int j=i+1;j<=n;j++)
    19         {
    20             if(a[i][1]<=a[j][1]&&a[j][2]>l)//如果它后面的数比它大并且找一个长度最长的 
    21             {
    22                 l=a[j][2];
    23                 k=j;
    24             }
    25             if(l>0)
    26             {
    27                a[i][2]=l+1;//更新长度 
    28                a[i][3]=k;//记录前驱 
    29             }
    30         }
    31     }
    32     int k=1;
    33     for(int i=1;i<=n;i++)
    34     if(a[i][2]>a[k][2])k=i;//找出长度最长的 
    35     cout<<a[k][2]<<endl;
    36     while(k!=0)
    37     {
    38         cout<<a[k][1]<<" ";//输出最长子序列 
    39         k=a[k][3];
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    exp 和imp 与expdp / impdp 区别
    nginx优化
    nginx root alias proxypass
    mysql3
    logrotate 用法
    SQL执行顺序
    http与RPC的关系
    docker
    windows 时间同步
    Java生成指定长度的随机字符串
  • 原文地址:https://www.cnblogs.com/zzyh/p/6669841.html
Copyright © 2011-2022 走看看