Cod sursa(job #917486)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 17 martie 2013 22:45:23
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cctype>
#define NMAX 100100

using namespace std;


int n, sol, poz, CNT;
int a[NMAX], v[NMAX], no[NMAX];
int aib[NMAX], scmax[NMAX];
int solution[NMAX];

inline int bsNorm(int number)
{
	int i = 0, cnt = CNT;
	for (; cnt > 0; cnt >>= 1)
		if (i + cnt <= n)
			if (v[i + cnt] <= number)
				i += cnt;
	return i;
}
inline int query(int value)
{
	int ret = 0;
	for (int i = value; i >= 1; i = i & (i - 1))
		ret = max(ret, aib[i]);
	return ret;
}
inline void update(int position, int value)
{
	for (int i = position; i <= NMAX; i = (i | (i - 1)) + 1)
		aib[i] = max(aib[i], value);
}

const int BSZ = 4096 * 1024;
char buff[BSZ];
inline int nextInt()
{
	static int I = BSZ;
	int ret = 0;
	if (I == BSZ)
		fread(buff, 1, BSZ, stdin), I = 0;
	while (!isdigit(buff[I]))
		if (++I == BSZ)
			fread(buff, 1, BSZ, stdin), I = 0;
	while (isdigit(buff[I]))
	{
		ret = ret * 10 + buff[I] - '0';
		if (++I == BSZ)
			fread(buff, 1, BSZ, stdin), I = 0;
	}
	return ret;
}

string out;
inline string tostring(int number)
{
	if (number == 0) return "";
	return tostring(number / 10) + (char)(number % 10 + '0');
}


int main()
{
	freopen("scmax.in", "r", stdin);
	freopen("scmax.out", "w", stdout);
	n = nextInt();
	for (int i = 1; i <= n; i++)
		a[i] = nextInt(), v[i] = a[i], no[i] = a[i];
	sort(v + 1, v + n + 1);
	for (CNT = 1; CNT < n; CNT <<= 1);
	for (int i = 1; i <= n; i++)
		a[i] = bsNorm(a[i]);
	
	for (int i = 1; i <= n; i++)
	{
		scmax[i] = query(a[i] - 1) + 1;
		update(a[i], scmax[i]);
		if (sol < scmax[i])
			sol = scmax[i], poz = i;
	}
	
	for (int i = poz; i >= 1; i--)
	{
		if (a[i] < a[poz] && scmax[i] + 1 == scmax[poz])
			out = tostring(no[poz]) + " " + out, poz = i;
	}
	out = tostring(sol) + "\n" + tostring(no[poz]) + " " + out;
	cout << out;
}