Pagini recente » Cod sursa (job #2446476) | Cod sursa (job #1570698) | Cod sursa (job #1013031) | Cod sursa (job #221272) | Cod sursa (job #2524748)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int MaxN = 100100;
int a[MaxN], best[MaxN], pozitii[MaxN];
int n;
int maxim = 0, poz_start;
void Citire();
void Construieste_Best();
int main()
{
Citire();
Construieste_Best();
fout << maxim << '\n';
int max_copy = maxim;
while (max_copy--)
{
fout << a[poz_start] << ' ';
poz_start = pozitii[poz_start];
}
}
void Construieste_Best()
{
best[n] = 1;
for (int i = n - 1; i >= 1; i--)
{
best[i] = 1;
for (int j = i + 1; j <= n; ++j)
{
if (a[i] < a[j] && best[i] < best[j] + 1)
{
best[i] = best[j] + 1;
pozitii[i] = j;
}
}
if (best[i] > maxim)
{
maxim = best[i];
poz_start = i;
}
}
}
void Citire()
{
fin >> n;
for (int i = 1; i <= n; ++i)
fin >> a[i];
}