UVa481 What Goes Up

来源:互联网 发布:redis php扩展 编辑:程序博客网 时间:2024/06/10 22:45
  • 题目大意:求LIS,同时输出该序列。

  • 思路:发现了一个叫lower_bound( )的函数,直接用它二分即可。不过要输出序列,需要记录各元素在二分中的序列中的位置(即以该元素为尾的LIS长度)。然后从后往前找一遍,第一个出现的代表某长度的元素即为序列中的元素(因为该元素最晚被更新),输出即可。

  • 代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100005;int l=0,a[maxn],c[maxn],pos[maxn],ori[maxn];bool vis[maxn];void work(){     int len=0;     c[0]=-10000;     for (int i=1;i<=l;++i)     {         if (a[i]>c[len])         {             len++;             pos[i]=len;             c[len]=a[i];         }         else         {             int p=lower_bound(c+1,c+len+1,a[i])-c;             pos[i]=p;             c[p]=a[i];         }     }     cout<<len<<endl<<'-'<<endl;     int p=len,from;     bool flag=0;     for (int i=l;i>=1;--i)     {         if (pos[i]==len)         {             from=i;             break;         }     }     ori[len]=a[from];     p--;     for (int i=from-1;i>=1;--i)     {         if (pos[i]==p && a[i]<a[from])         {             from=i;             ori[p]=a[i];             p--;         }     }     for (int i=1;i<=len;++i)       cout<<ori[i]<<endl;}void init(){     int tmp;     memset(vis,0,sizeof(vis));     while (scanf("%d",&tmp)==1)     {           a[++l]=tmp;     }}int main(){    init();    work();    return 0;}
0 0
原创粉丝点击