题目描述 Description
给一个数组a1, a2 ... an,找到最长的上升降子序列ab1<ab2< .. <abk,其中b1<b2<..bk。
输出长度即可。
输入描述 Input Description
第一行,一个整数N。
第二行 ,N个整数(N < = 5000)
输出描述 Output Description
输出K的极大值,即最长不下降子序列的长度
样例输入 Sample Input
5
9 3 6 2 7
样例输出 Sample Output
3
自己构建的一个按照 已经计算到的最大上升子序列个数 排列的优先级队列。成功AC.
#include<queue>
#include<iostream>
using namespace std;
class Node{
public:
Node (int ii,int aa,int vv){
index=ii,ans=aa,value=vv;
nt=NULL;
}
int index,ans,value;
Node *nt;
}head(-1,-1,-1);
int main(){
int n;cin>>n;
int t;
for(int i=0;i<n;i++){
cin>>t;
Node *newNode=new Node(i,1,t);
Node *q=&head;//代表p的前一个指针
Node *p=&head;
p=p->nt;
while(p!=NULL){
if(t>p->value){
newNode->ans=p->ans+1;
break;
}
p=p->nt;
}
q=&head;//代表p的前一个指针
p=&head;
p=p->nt;
while(p!=NULL){
if(newNode->ans>p->ans){
break;
}
q=p;
p=p->nt;
}
if(p==NULL){
q->nt=newNode;
}else{
newNode->nt=p;
q->nt=newNode;
}
}
if(head.nt==NULL)cout<<0<<endl;
else cout<<head.nt->ans<<endl;
return 0;
}