Cod sursa(job #2510791)

Utilizator Robert_VRVRobert Vadastreanu Robert_VRV Data 17 decembrie 2019 14:44:30
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <algorithm>
#include <fstream>

const int MAX_N = 100005;

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

int n, a[MAX_N];
int AIB[MAX_N], best[MAX_N];
int last[MAX_N], ord[MAX_N], aux[MAX_N];

void update(int pos, int val) {
  for (int i = pos; i <= n; i += i & (-i))
    if (best[val] > best[AIB[i]])
      AIB[i] = val;
}

int query(int pos) {
  int max = 0;
  for (int i = pos; i > 0; i -= i & (-i))
    if (best[AIB[i]] > best[max])
      max = AIB[i];
  return max;
}

void afis(int pos) {
  if (pos != 0) {
    afis(last[pos]);
    fout << a[pos] << " ";
  }
}

int main() {

  fin >> n;
  for (int i = 1; i <= n; i++) {
    fin >> a[i];
    aux[i] = a[i];
  }
  std::sort(aux + 1, aux + n + 1);
  int nr = 1;
  for (int i = 2; i <= n; i++)
    if (aux[i] != aux[nr])
      aux[++nr] = aux[i];
  for (int i = 1; i <= n; i++)
    ord[i] = std::lower_bound(aux + 1, aux + nr + 1, a[i]) - aux;
  for (int i = 1; i <= n; i++) {
    last[i] = query(ord[i] - 1);
    best[i] = best[last[i]] +  1;
    update(ord[i], i);
  }
  int sol = 0;
  for (int i = 1; i <= n; i++)
    if (best[sol] < best[i])
      sol = i;
  fout << best[sol] << '\n';
  afis(sol);

  return 0;
}