Cod sursa(job #2679716)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 1 decembrie 2020 12:49:12
Problema Subsir crescator maximal Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

std::ofstream out("scmax.out");

const int NMAX = 1e5;
const int INF = 1e9 + 7;

int n;

int a[1 + NMAX];

int ord[1 + NMAX];

int prev[1 + NMAX];

void read() {
  std::ifstream in("scmax.in");

  in >> n;

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

  in.close();
}

int bs(int ind) {
  int left = 1, right = n, mid, pos = 0;

  while (left <= right) {
    mid = (left + right) / 2;

    if (a[ord[mid]] < a[ind]) {
      pos = mid;
      left = mid + 1;
    }
    else
      right = mid - 1;
  }


  return pos;
}

void rec(int pos) {
  if (pos != 0) {
    rec(prev[pos]);
    out << a[pos] << ' ';
  }
}

int main() {
  int len_max = 0;

  read();

  a[0] = INF;

  for (int i = 1; i <= n; ++i) {
    int ind = bs(i);

    ord[ind + 1] = i;
    prev[i] = ord[ind];

    len_max = std::max(len_max, ind + 1);
  }

  out << len_max << '\n';

  int pos = ord[len_max];
  rec(pos);

  return 0;
}