Cod sursa(job #3201633)

Utilizator Toni07Stoica Victor Toni07 Data 9 februarie 2024 11:41:44
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
const int nmax=1e6+3;
int best[nmax], a[nmax], poz[nmax], mx, sol=0, p, n;
void dp() {
  int i,j;
  best[n]=1;
  poz[n]=-1;
  mx=1;
  p=n;
  for(i=n-1;i>=1;--i){
   best[i]=1;
   poz[i]=-1;
   for(j=i+1;j<=n;++j)
       if(a[i]<a[j] && best[i]<=best[j]){
         best[i]=best[j]+1;
         poz[i]=j;
         if(best[i]>mx) mx=best[i],p=i;
         }
    }
    fout << mx << "\n";
  }
void constr(){
    int i;
    i=p;
    while(i!=-1){
        fout << a[i] << " ";
        i=poz[i];
        }
    }
int main(){
    fin >> n;
    for (int i=1;i<=n;++i) fin >> a[i];
    dp();
    constr();
    return 0;
  }