Cod sursa(job #1909367)

Utilizator lflorin29Florin Laiu lflorin29 Data 7 martie 2017 12:29:27
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;

const int nMax = 1e5 + 3;

const int INF = 1e8;

int n;

struct MaxFenwickTree {
	int aib[nMax];
	MaxFenwickTree() {
		memset(aib, 0, sizeof(aib));
	}
	int lsb(int x) {
		return x & -x;
	}
	void upd(int pos, int val) {
		for (; pos <= n; pos += lsb(pos))
			aib[pos] = max(aib[pos], val);
	}
	int query(int pos) {
		int ans = 0;

		for (; pos > 0; pos -= lsb(pos))
			ans = max(ans, aib[pos]);

		return ans;
	}
};

MaxFenwickTree AibMax;
pair<int, int>a[nMax];
int dp[nMax];

void citire() {
	scanf("%d", &n);

	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i].first);
}

void Normalizare() {
	vector<int>aux;

	for (int i = 1; i <= n; ++i)
		aux.push_back(a[i].first);

	sort(aux.begin(), aux.end());
	aux.resize(unique(aux.begin(), aux.end()) - aux.begin());

	for (int i = 1; i <= n; ++i)
		a[i].second = lower_bound(aux.begin(), aux.end(), a[i].first) - aux.begin() + 1;//id de la 1
}

void Print(int pos) {
	for (int i = pos - 1; i >= 1; --i) {
		if (a[i].first < a[pos].first and dp[i] == dp[pos] - 1) {
			Print(i);
			break;
		}
	}

	printf("%d ", a[pos].first);
}
void Calclis() {
	pair<int, int>sol(0, 0);

	for (int i = 1; i <= n; ++i) {
		int val = AibMax.query(a[i].second - 1) + 1;
		AibMax.upd(a[i].second, val);

		if (val > sol.first)
			sol.first = val, sol.second = i;

		dp[i] = val;
	}

	printf("%d\n", sol.first);
	Print(sol.second);
}
int main() {
	freopen("scmax.in", "r", stdin);
	freopen("scmax.out", "w", stdout);

	citire();
	Normalizare();
	Calclis();
	return 0;
}