Cod sursa(job #2835177)

Utilizator ViAlexVisan Alexandru ViAlex Data 18 ianuarie 2022 10:44:30
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include<iostream>
#include<fstream>
using namespace std;

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

const int mx=1e5+100;
const int inf=2e9+100;

// v[i] - the input array
// d[i] - the minimum end element of a sequence of length i 
// m[i] - the index of the minimum end element of a sequence of length i 
// p[i] - index of the previous element in the sequence that ends in i 
// r - result

int n,v[mx],d[mx],m[mx],p[mx],r[mx];



void read(){
	in>>n;
	for(int i=0;i<n;i++){
		in>>v[i];
	}
}

int search(int bound){
	int l=0,r=n,m;	
	while(l<r){
		m=(l+r)/2;	

		if(d[m]<=bound){
			l=m+1;
		}
		else{
			r=m;
		}

	}
	return l;
}

void solve(){
	d[0]=-inf;
	m[0]=-1;

	for(int i=1;i<=n;i++){
		d[i]=inf;
	}

	for(int i=0;i<n;i++){
		int j=search(v[i]);
		if(d[j-1]<v[i]){
			d[j]=v[i];
			m[j]=i;
			p[i]=m[j-1];
		}
	}	
	
	int maximum=-1;
	for(int i=1;i<=n;i++){
		if(d[i]!=inf){
			maximum=i;
		}
	}
	int now=m[maximum],cnt=0;

	while(now!=-1){
		r[cnt++]=v[now];
		now=p[now];
	}

	out<<maximum<<endl;
	for(int i=maximum-1;i>=0;i--)
		out<<r[i]<<" ";
}


int main(){
	read();
	solve();

	return 0;
}