Cod sursa(job #2526478)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 18 ianuarie 2020 18:09:36
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

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

//VARIABLES
int n;
int v[100005];
int dp[100005];
int last[100005];
int MAX;
vector <int> ord;

//FUNCTIONS

int cautbin(int i) {
	int st = 0; int dr = i - 1;
	int ans = 0;

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

	return ans;
}

//MAIN
int main() {

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

	for (int i = 1; i <= n; i++) {
		int ans = cautbin(i);

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

	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 << " ";

	}

	return 0;
}