Cod sursa(job #2679490)

Utilizator RamanujanNeacsu Mihnea Ramanujan Data 30 noiembrie 2020 17:30:22
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
long long a[MAXN], LIS[MAXN], prec[MAXN];
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int main()
{
    int n; fin>>n;
    for(int i=0; i<n; i++)
      fin>>a[i];
    for(int i=0; i<n; i++)
    {
       if(i==0) LIS[i]=1;
       else
       {
           int MAX=0, before=0;
           for(int j=i-1; j>=0; j--)
              if(a[j]<a[i]&&LIS[j]>MAX)
                MAX=LIS[j], before=j;
           LIS[i]=MAX+1;
           prec[i]=before;
       }
    }
    int most=0, cap=0;
    for(int i=0; i<n; i++)
      if(LIS[i]>most) most=LIS[i], cap=i;
    fout<<most<<'\n';
    int start=LIS[cap];
    stack<long long> s;
    while(start>0)
    {
        s.push(a[cap]);
        cap=prec[cap];
        start--;
    }
    while(!s.empty())
    {
       fout<<s.top()<<" ";
       s.pop();
    }
    return 0;
}