Cod sursa(job #2575940)

Utilizator dahaandreiDaha Andrei Codrin dahaandrei Data 6 martie 2020 16:19:53
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <stack>

using namespace std;

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

const int MAXN = 1e6;
const int MAXVAL = 1e9;

int n;
int v[MAXN + 1], pre[MAXN + 1], lung[MAXN + 1];

struct tip {
	int poz, val;
	tip(int _poz = 0, int _val = 0) {
		poz = _poz;
		val = _val;
	}
}dp[MAXN + 1];

int cb(int val) {
	int ret = 0;
	int add = 1 << 20;
	while (add) {
		if (ret + add <= n && dp[ret + add].val < val) ret += add;
		add >>= 1;
	}
	return dp[ret].poz;
}

int main() {
	in >> n;
	v[MAXN + 1] = MAXVAL;
	for (int i = 1; i <= n; ++ i) {
		in >> v[i];
		dp[i].val = MAXVAL;
		lung[i] = 1;
	}

	int ans = 0;
	for (int i = 1; i <= n; ++ i) {
		pre[i] = cb(v[i]);
		lung[i] = lung[pre[i]] + 1;
		ans = max(ans, lung[i]);
		if (v[i] < dp[lung[i]].val) {
			dp[lung[i]].val = v[i];
			dp[lung[i]].poz = i;
		}
	}

	stack<int> afis;
	afis.push(dp[ans].poz);
	while (pre[afis.top()] != 0) {
		afis.push(pre[afis.top()]);
	}

	out << ans << '\n';
	while (afis.size()) {
		out << v[afis.top()] << ' ';
		afis.pop();
	}


	return 0;
}