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

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
", 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 }
"Hello World!"
原文地址:https://www.cnblogs.com/Aze-qwq/p/9337755.html