Cod sursa(job #2374528)

Utilizator GoogalAbabei Daniel Googal Data 7 martie 2019 19:10:18
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 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 res = 0;

  while(step != 0) {
    if(res + step <= m && a[b[res + step]] < x)
      res += step;

    step /= 2;
  }

  return res;
}

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;
}