Cod sursa(job #2280072)

Utilizator Catalin_BorzaBorza Catalin-Mihai Catalin_Borza Data 10 noiembrie 2018 11:13:44
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#define NMAX 100005
using namespace std;

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

long long a[NMAX], b[NMAX];
int n, kpos = 1, nk = 1, pos[NMAX];

int find_pos(long long x, int st, int dr)
{
	if (st >= dr) return st;
	int m = (st + dr) >> 1;
	if (a[m] > x)
		return find_pos(x, st, m - 1);
	if (a[m] == x) return m;
	return find_pos(x, m + 1, dr);
}

void out_array(int i, int k)
{
	if (i < 0 || k == 0)
		return;
	if (pos[i] == k)
	{
		out_array(i - 1, k - 1);
		g << b[i] << " ";
	}
	else
		out_array(i - 1, k);
}

int main()
{
	long long x;
	f >> n >> a[0];
	pos[0] = 1;
	b[0] = a[0];
	for (int i = 1; i < n; i++)
	{
		f >> x;
		b[i] = x;
		if (x > a[nk - 1])
		{
			pos[kpos++] = nk + 1;
			a[nk++] = x;
		}
		else
		{
			pos[kpos] = find_pos(x, 0, nk - 1) + 1;
			a[pos[kpos++] - 1] = x;
		}
	}
	g << nk << "\n";
	int k = 1;
	out_array(kpos - 1, nk);

	return 0;
}