Cod sursa(job #2175125)

Utilizator GoogalAbabei Daniel Googal Data 16 martie 2018 15:28:15
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 1e5;

int n, m;
int a[1 + NMAX];
int b[1 + NMAX];
int p[1 + NMAX];

int bin_src(int x) {
  int step = 1 << 18;
  int r = 0;
  while(step != 0) {
    if(r + step <= m && a[b[r + step]] < x)
      r += step;
    step /= 2;
  }

  return r;
}

void write(int x) {
  if(0 < x) {
    write(p[x]);
    out << a[x] << ' ';
  }
}

int main()
{
  in >> n;
  for(int i = 1; i <= n; i++)
    in >> a[i];

  b[++m] = 1;
  for(int i = 2; i <= n; i++) {
    p[i] = 0;
    int k = bin_src(a[i]);
    p[i] = b[k];
    b[k + 1] = i;
    m = max(m, k + 1);
  }

  out << m << '\n';
  write(b[m]);
  out << '\n';

  in.close();
  out.close();
  return 0;
}