Cod sursa(job #2673955)

Utilizator PetyAlexandru Peticaru Pety Data 18 noiembrie 2020 11:55:52
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.31 kb
#include <fstream>
#include <map>
#include <algorithm>
#include <vector>

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

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long 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;
	}
};

using namespace std;

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

int n, a[100002], b[100002], nr, aib[100002], ind[100002], dp[100002], Norm[100002], ant[100002], y;
map<int, int>mp;

void update (int x, int val, int j) {
  for (int i = x; i <= nr; i += (i & -i)) {
    if (aib[i] < val) {
      aib[i] = val;
      ind[i] = j;
    }
  }
}

int query (int x, int &poz) {
  int ans = 0;
  for (int i = x; i; i -= (i & -i)) {
    if (ans < aib[i]) {
      ans = aib[i];
      poz = ind[i];
    }
  }
  return ans;
}


int main()
{
  fin >> n;
  for (int i = 1; i <= n; i++) {
    fin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    if (i == 1 || b[i] != b[i - 1]) {
      mp[b[i]] = ++nr;
      Norm[nr] = b[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i] = mp[a[i]];
    int last = 0;
    dp[i] = 1 + query(a[i] - 1, last);
    ant[i] = last;
    update(a[i], dp[i], i);
  }
  int mx = query(nr, y);
  fout << mx << "\n";
  for (int i = 1; i <= n; i++) {
    if (dp[i] == mx) {
      int poz = i;
      vector<int>ans;
      while (poz) {
        ans.push_back(Norm[a[poz]]);
        poz = ant[poz];
      }
      reverse(ans.begin(), ans.end());
      for (auto it : ans)
        fout << it << " ";
      return 0;
    }
  }
  return 0;
}