Pagini recente » Cod sursa (job #3122247) | Cod sursa (job #1375140) | Cod sursa (job #2982512) | Cod sursa (job #2357164) | Cod sursa (job #1853308)
#include <iostream>
#include <fstream>
using namespace std;
int n, v[100005], best[100005], maxL, poz_maxL, poz[100005];
void read()
{
ifstream fin("scmax.in");
fin >> n;
for(int i = 1; i <= n; ++i)
fin >> v[i];
}
int max_best(int x)
{
int mx = -1;
for(int i = x + 1; i <= n; ++i)
if(v[i] > v[x] && best[i] > mx){
mx = best[i];
poz[x] = i;
}
return mx;
}
void solve()
{
best[n] = 1;
poz[n] = -1;
for(int i = n - 1; i >= 1; --i){
best[i] = max_best(i) + 1;
if(best[i] > maxL){
maxL = best[i];
poz_maxL = i;
}
}
}
void write()
{
ofstream fout("scmax.out");
fout << maxL << '\n';
for(int i = 1; i <= maxL; ++i){
fout << v[poz_maxL] << ' ';
poz_maxL = poz[poz_maxL];
}
}
int main()
{
read();
solve();
write();
return 0;
}