Cod sursa(job #2521874)

Utilizator Alex18maiAlex Enache Alex18mai Data 11 ianuarie 2020 17:22:31
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
//ALEX ENACHE

#include <vector>
#include <algorithm>

#include <math.h>
#include <iomanip>
#include <bitset>

#include <queue>
#include <deque>
#include <stack>

#include <map>
#include <unordered_map>
#include <set>
#include <unordered_map>

#include <random>
#include <time.h>
#include <assert.h>

using namespace std;

//-----------------------------------------------------------------

//#include <iostream> 
//#pragma warning(disable:4996)

#include <fstream>
ifstream cin("scmax.in"); ofstream cout("scmax.out");
//ifstream cin("input"); ofstream cout("output");

const int inf = 2e9 + 1;
const int MAXN = 1e5;

int n;
int v[MAXN + 5];
int dp[MAXN + 5];
int last[MAXN + 5];
int MAX;
vector < int > ord;

void read() {
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> v[i];
		dp[i] = n + 1;
	}
	v[n + 1] = inf;
}

int caut_bin(int i) {
	int st = 0, dr = i - 1;
	int ans = 0;
	while (st <= dr) {
		int mij = st + dr;
		mij /= 2;
		if (v[i] > v[dp[mij]]) {
			ans = mij;
			st = mij + 1;
		}
		else {
			dr = mij - 1;
		}
	}
	return ans;
}

void solve() {
	for (int i = 1; i <= n; i++) {
		int ans = caut_bin(i);

		if (v[i] < v[dp[ans + 1]]) {
			dp[ans + 1] = i;
		}
		last[i] = dp[ans];
		MAX = max(MAX, ans + 1);
	}
}

void afisare() {
	cout << MAX << '\n';
	int now = dp[MAX];
	while (now) {
		ord.push_back(v[now]);
		now = last[now];
	}
	reverse(ord.begin(), ord.end());
	for (auto& x : ord) {
		cout << x << " ";
	}
	cout << '\n';
}

int main() {

	//freopen("input", "r", stdin); freopen("output", "w", stdout);

	read();
	solve();
	afisare();

	return 0;
}