Cod sursa(job #3334290)

Utilizator matiasmatias matias Data 17 ianuarie 2026 10:15:03
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.89 kb
#include <vector>
#include <algorithm>
#include <stack>
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace std;

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

unordered_map<int, int> norm;
const int Vmax = 2e9;
const int Nmax = 1e5 + 1;
int aib[Nmax], n;


void update(int idx, int val) {
	for (int i = idx; i <= n; i += (i & (-i))) {
		aib[i] = max(aib[i], val);
	}


}


int query(int x) {
	int maxi = 0;
	for (int i = x; i >= 0; i -= i & (-i)) {

		maxi = max(maxi, aib[i]);
	}
	return maxi;


}


int v[Nmax], c[Nmax], ans[Nmax];



int main()
{
	

	fin >> n;
	int maxi = 0;
	for (int i = 1; i <= n; i++) {
		fin >> v[i];
		c[i] = v[i];
	}
	sort(c + 1, c + n + 1);
	int i = 1, poz = 1;
	for (int i = 1; i <= n; i++) {
		while (i < n && c[i] == c[i + 1])i++;
		norm[c[i]] = poz++;

	}

	for (int i = 1; i <= n; i++) {
		c[i] = norm[v[i]];
		ans[i] = query(c[i] - 1) + 1;
		maxi = max(maxi, ans[i]);
		update(c[i], ans[i]);

	}

	fout << maxi << '\n';
	stack <int>lis;
	for (int i = n; i; i--) {
		if (ans[i] == maxi) {
			maxi--;
			lis.push(v[i]);
		}
	}
	while (!lis.empty()) {
		fout << lis.top() << " ";
		lis.pop();


	}

	fin.close();
	fout.close();
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file