Cod sursa(job #698250)

Utilizator Catah15Catalin Haidau Catah15 Data 29 februarie 2012 13:04:32
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
// O (NlogN)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

#define maxN 100010
#define int64 long long
#define PB push_back

int64 V[maxN];
int best[maxN], poz[maxN], ant[maxN];
int dim, ans, fin;
vector <int64> sol;


int bin_Search (int lo, int hi, int val)
{
	int res = 0;
	
	while (lo <= hi)
	{
		int mid = (lo + hi) / 2;
		
		if (V[poz[mid]] < val)
		{
			res = max (res, mid);
			lo = mid + 1;
		}
		else hi = mid - 1;
	}
	
	return res;
}


int main()
{
	freopen ("scmax.in", "r", stdin);
	freopen ("scmax.out", "w", stdout);
	
	int N;
	
	scanf ("%d", &N);
	
	for (int i = 1; i <= N; ++ i)
	{
		scanf ("%lld", &V[i]);
		
		int lm = bin_Search (1, dim, V[i]);
		
		best[i] = lm + 1;
		ant[i] = poz[lm];
		
		poz[lm + 1] = i;
		
		if (lm + 1 > dim) ++ dim;
		if (best[i] > ans)
		{
			ans = best[i];
			fin = i;
		}
	}
	
	printf ("%d\n", ans);
	
	for (int i = fin; i; i = ant[i]) sol.PB (V[i]);
	for (int i = sol.size() - 1; i >= 0; -- i) printf ("%lld ", sol[i]);
	
	return 0;
}