Cod sursa(job #2510790)

Utilizator Robert_VRVRobert Vadastreanu Robert_VRV Data 17 decembrie 2019 14:41:15
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <algorithm>
#include <stdio.h>

const int MAX_N = 100000;

int n, a[5 + MAX_N];
int AIB[5 + MAX_N], best[5 + MAX_N];
int last[5 + MAX_N], ord[5 + MAX_N], aux[5 + 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]);
    printf("%d ", a[pos]);
  }
}

int main() {

  freopen("scmax.in", "r", stdin);
  freopen("scmax.out", "w", stdout);

  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &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;
  printf("%d\n", best[sol]);
  nr = 0;
  for (int i = sol; i > 0; i = last[i])
		aux[++nr] = a[i];
	for (int i = nr; i > 0; i--)
		printf("%d ", aux[i]);
	printf("\n");

  return 0;
}