Cod sursa(job #2907108)

Utilizator BadBoyRADULICEA Constantin Dumitru Petre BadBoy Data 28 mai 2022 19:06:19
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>

//https://infoarena.ro/problema/scmax

int main()
{
	std::vector<int> nums;
	std::vector<int> len;
	std::vector<int> prev;
	std::ifstream fin("scmax.in");
	std::ofstream fout("scmax.out");
	int n_nums, len_max, indx_max, indx;

	fin >> n_nums;
	nums.resize(n_nums);
	len.resize(n_nums);
	prev.resize(n_nums);

	for (int i = 0; i < n_nums; i++)
		fin >> nums[i];

	for (int i = n_nums - 1; i >= 0; i--)
	{
		len[i] = 1;
		len_max = 0;
		indx = -1;
		for (int j = i + 1; j < n_nums; j++)
		{
			if (nums[i] < nums[j] && len[j] > len_max) {
				len_max = len[j];
				indx = j;
			}
		}
		if (indx != -1) len[i] += len[indx];
		prev[i] = indx;
	}

	len_max = 0;
	indx_max = -1;

	for (int i = 0; i < n_nums; i++)
	{
		if (len[i] > len_max) {
			len_max = len[i];
			indx_max = i;
		}
	}

	fout << len_max << '\n';
	printf("%d\n", len_max);
	while (indx_max != -1)
	{
		fout << nums[indx_max] << ' ';
		printf("%d ", nums[indx_max]);
		indx_max = prev[indx_max];
	}
	fin.close();
	fout.close();
	return 0;
}