Cod sursa(job #1166379)

Utilizator toranagahVlad Badelita toranagah Data 3 aprilie 2014 15:24:12
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

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

const int MAX_N = 100100;
#define len(x) int((x).size())

int N;
int v[MAX_N];
int p[MAX_N];

void read();
void scmax();
void rebuild(int pos);

int main() {
  read();
  scmax();
  return 0;
}

void read() {
  fin >> N;
  for (int i = 1; i <= N; i += 1)
    fin >> v[i];
}

void scmax() {
  vector<int> l;
  l.push_back(0);
  for (int i = 1, p2 = 1, pos; i <= N; i += 1) {
    if (v[l.back()] < v[i]) {
      p[i] = l.back();
      l.push_back(i);
      continue;
    }

    while ((p2 << 1) < len(l)) p2 <<= 1;
    pos = 0;
    for (int step = p2; step; step >>= 1)
      if (pos + step < len(l) && v[l[pos + step]] < v[i])
        pos += step;


    p[i] = l[pos];
    l[pos + 1] = i;
  }

  fout << len(l) - 1 << '\n';
  rebuild(l.back());
}

void rebuild(int pos) {
  if (pos == 0) return;
  rebuild(p[pos]);
  fout << v[pos] << ' ';
}