Cod sursa(job #2328873)

Utilizator BGondaGonda George Luis Bogdan BGonda Data 26 ianuarie 2019 11:11:39
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
using namespace std;

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

const int MaxN = 100001, Inf = 0x3f3f3f3f;
int v[MaxN];     
int a[MaxN];     
int p[MaxN];
int subs[MaxN]; 
int n, x;
int Lmax;

int Bs(int x);

int main()
{
	fin >> n;
	v[0] = -Inf;
	for (int i = 1; i <= n; ++i)
	{
		fin >> a[i];
		int poz = Bs(a[i]);
		v[poz] = a[i];
		p[i] = poz;
		
	}
	
	fout << Lmax << '\n';
	int i = n, k = 0;
	for (int L = Lmax; L >= 1; --L)
		for (; i >= 1; --i)
			if (p[i] == L)
			{
				subs[++k] = a[i];
				break;
			}
			
	for (int i = k; i >= 1; --i)
		fout << subs[i] << ' ';
}  

int Bs(int x)
{
	if (x > v[Lmax]) // sirul nu e strict crescator
	{
		Lmax++;
		return Lmax;
	}
	
	int st = 1, dr = Lmax, mj, poz;
	
	while (st <= dr)
	{
		mj = (st + dr) / 2; // m = (st + (dr - st) / 2)
		if (x > v[mj])
			st = mj + 1;
		else
		{
			poz = mj;
			dr = mj - 1;
		}
	}
	
	return poz;
}