Cod sursa(job #2553172)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 21 februarie 2020 18:21:50
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

int n;
int ve[100041];

vector<int> v;
int lg = 1;
int bs(int a){
	while(lg <= v.size()){
		lg <<= 1;
	}
	
	int r = -1;
	for(int i = lg; i > 0; i>>=1){
		if(r+i < v.size() && a>v[r+i]){
			r += i;
		}
	}
	return r+1;
}

int dp[100041];
int maxi;
void sic(){
	for(int i = 0; i < n; ++i){
		int ps = bs(ve[i]);
		dp[i] = ps+1;
		maxi = max(maxi, dp[i]);
		if(ps == v.size()){
			v.push_back(ve[i]);
		}else{
			v[ps] = ve[i];
		}
	}
}

stack<int> sol;
void sho(){
	fout << maxi << "\n";
	for(int i = n-1; i >= 0; --i){
		if(dp[i] == maxi){
			sol.push(ve[i]);
			maxi--;
		}
	}
	while(!sol.empty()){
		fout << sol.top() << " ";
		sol.pop();
	}
}

int main(){
	fin >> n;
	for(int i = 0; i < n; ++i){
		fin >> ve[i];
	}
	sic();
	sho();
	return 0;
}