Cod sursa(job #533388)

Utilizator sandyxpSanduleac Dan sandyxp Data 13 februarie 2011 20:33:28
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

int bsearch(int *A, int size, int elem) {
  int i, step;
  for (step = 1; step < size; step <<= 1);
  for (i = size; step; step >>= 1) {
    if (i-step >= 0 && elem <= A[i-step]) {
      i -= step;
    }
  }
  return i;
}

int main() {
  int n;
  freopen("scmax.in", "r", stdin);
  freopen("scmax.out", "w", stdout);

  scanf("%d", &n);
  int a;
  int *A = new int[n];
  int *B = new int[n];
  int size = 0;
  int size_max = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    int pos = bsearch(A, size, a);
    A[pos] = a;
    if (size == pos) {
      size ++;
    }
    if (size > size_max) {
      size_max = size;
      memcpy(B, A, sizeof(int)*size);
    }
  }
  printf("%d\n", size_max);
  for (int i = 0; i < size_max; ++i) {
    printf("%d ", B[i]);
  }
  printf("\n");
  return 0;
}