码迷,mamicode.com
首页 > 其他好文 > 详细

模板:最长上升子序列(LIS)

时间:2018-07-19 20:57:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:using   lis   pac   blank   top   target   upper   ref   pre   

http://lfyzit.com/problem/242

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<stack>
 5 using namespace std;
 6 int d[205], c[205], a[205], len=1, n=0;
 7 int main(){
 8     int x;
 9     while(cin>>x) {
10         a[++n]=x;
11     }
12 
13     d[1]=a[1];
14     c[1]=1;
15     for(int i=2; i<=n; ++i) {
16         if(d[len]<a[i]) {
17             d[++len]=a[i];
18             c[i]=len;
19         } 
20         else if(d[len]>a[i]){
21             int j=upper_bound(d+1, d+len+1, a[i])-d;
22             d[j]=a[i];
23             c[i]=j;
24         }
25     }
26     stack<int> s;
27     for(int i=n, j=len; i>=1; --i) {
28         if(c[i]==j) {
29             s.push(a[i]); 
30             --j;
31         }
32         if(j==0) break;
33     }
34     printf("max=%d\n", len);
35     while(!s.empty()){
36         if(s.size()>1){
37             printf("%d ",s.top());
38             s.pop();}
39         else if(s.size()==1){
40             printf("%d",s.top());
41             s.pop();
42         }
43     }
44     return 0;
45 }

 

模板:最长上升子序列(LIS)

标签:using   lis   pac   blank   top   target   upper   ref   pre   

原文地址:https://www.cnblogs.com/Aze-qwq/p/9337755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!