Cod sursa(job #831425)

Utilizator GodiesVlad Voicu Godies Data 8 decembrie 2012 17:05:36
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>

int x[100002], m[100002], xpoz[100002];
int L = 0, n;

using namespace std;

int findlargest(int i)
{
	int low = 1, high = L;
	bool found = 0;
	int poz;
	while (low <= high) {
		int med = low + (high - low) / 2;
		if (x[m[med]] < x[i]) {
			found = 1;
 			low = med + 1;
 			poz = med;
		} else {
			high = med - 1;
		}
	}
	
	if (found) {
		return poz;
	}
	return 0;
}

int main() {
	FILE *f = fopen("scmax.in", "rt");
	FILE *g = fopen("scmax.out", "wt");
	fscanf(f, "%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		fscanf(f, "%d", &x[i]);
	}
	for (int i = 1; i <= n; ++i)
	{
		int j = findlargest(i);
		xpoz[i] = m[j];
 		if ((j == L) || (x[i] < x[m[j+1]])) {
			m[j + 1] = i;
			L = max(L, j+1);
 		}
	}
	int poz = m[L];
	vector<int> results;
	for (int i = 0; i < L; ++i)
	{
		results.push_back(x[poz]);
		poz = xpoz[poz];
	}
	fprintf(g, "%d\n", L);
	for (std::vector<int>::reverse_iterator it = results.rbegin(); it != results.rend(); ++it)
	{
		fprintf(g, "%d ", *it);
	}
	fclose(f);
	fclose(g);
	return 0;
}