Cod sursa(job #2339775)

Utilizator dahaandreiDaha Andrei Codrin dahaandrei Data 9 februarie 2019 11:54:03
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>

using namespace std;

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

const int MAXN = 1e5;
const int MAXVAL = 2e9;

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

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

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

	pre[poz] = dp[ret].poz;
	if (val < dp[ret + 1].val) {
		dp[ret + 1].val = val;
		dp[ret + 1].poz = poz;
	}
	ans = max(ans, ret + 1);
}

void afisare(int poz) {
	if (poz != 0) {
		afisare(pre[poz]);
		out << v[poz] << ' ';
	}
}

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

	for (int i = 1; i <= n; ++ i) cb(v[i], i);
	out << ans << '\n';
	afisare(dp[ans].poz);

	return 0;
}