Cod sursa(job #3230791)

Utilizator ClassicalClassical Classical Data 22 mai 2024 20:22:27
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 100000 + 7;
int n, a[N], dp[N], b[N];

void print(int i) {
	if (dp[i] == 1) {
		cout << a[i] << " ";	
		return;
	}
	int j = i - 1;
	while (!(dp[j] + 1 == dp[i] && a[j] < a[i])) {
		j--;
	}
	print(j);
	cout << a[i] << " ";	
}

int aib[N];

void upd(int i, int x) {
	while (i <= n) {
		aib[i] = max(aib[i], x);
		i += i & (-i);
	}
}

int get(int i) {
	int sol = 0;
	while (i) {
		sol = max(sol, aib[i]);
		i -= i & (-i);
	}
	return sol;
}

int main() {
#ifdef INFOARENA
	freopen ("scmax.in", "r", stdin);
	freopen ("scmax.out", "w", stdout);
#endif
	
	cin >> n;
	map<int, int> mp;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		mp[a[i]] = 0;
	}
	int y = 0;
	for (auto &it : mp) {
		it.second = ++y;
	}
	for (int i = 1; i <= n; i++) {
		b[i] = mp[a[i]];
	}
	for (int i = 1; i <= n; i++) {
		dp[i] = get(b[i] - 1) + 1;
		upd(b[i], dp[i]);
	}
	int ind = max_element(dp + 1, dp + n + 1) - dp;
	cout << dp[ind] << "\n";
	print(ind);
	
	return 0;
}