Cod sursa(job #2510793)

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

const int MAX_N = 100005;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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() {

  InParser fin("scmax.in");

  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;
}