Cod sursa(job #2398538)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 5 aprilie 2019 17:37:05
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
#define maxn 100000

using namespace std;

int v[maxn+5], cap[maxn+5], prew[maxn+5];

ifstream fin ( "scmax.in" );
ofstream fout ( "scmax.out" );

void sol ( int p )
{
  if ( prew[cap[p]] > 0 ) sol(prew[cap[p]]);
  fout << v[cap[p]] << ' ';
}

int main ()
{
  int n; fin >> n;
  int i, j, z, scm = 1, pas;
  for ( i = 1; i <= n; i++ ) fin >> v[i];

  for ( cap[1] = 1, i = 2; i <= n; i++ )
  {
    for ( j = 0, pas = (1<<17); pas > 0; pas /= 2 )
      if ( j + pas <= scm && v[cap[j+pas]] < v[i] ) j += pas;
    if ( j == 0 && v[cap[1]] > v[i] ) cap[1] = i;
    else if ( j >= 1 )
    {
      cap[j+1] = i;
      prew[i] = j;
      scm = max(scm, j+1);
    }
  }
  fout << scm << '\n';
  sol (scm);

  fin.close ();
  fout.close ();

  return 0;
}