Pagini recente » Cod sursa (job #89870) | Cod sursa (job #295524) | Solutii preONI 2006 - Runda a 2-a | Cod sursa (job #2097258) | Cod sursa (job #2037804)
#include <fstream>
#include <queue>
#include <stack>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
struct co
{
int nr, l, poz;
}z[100000];
stack <int> s;
int main()
{
int n, maxi=0, maxi2=0, dep;
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>z[i].nr;
z[i].l=1;
maxi=0;
if(i>=2)
{
int m=i-1, ct=1;
do
{
if(z[i].nr>z[m].nr)
{
int a=1, b=z[m].l;
if(maxi<(a+b))
{
maxi=a+b;
z[i].l=z[m].l+1;
z[i].poz=m;
}
}
if(z[i].nr<z[m].nr)
{
if(z[i].poz==0)
z[i].poz=i;
}
m--;
}while(m>=1);
if(maxi2<maxi)
{
maxi2=maxi;
dep=i;
}
}
else
{
z[i].l=1;
z[i].poz=1;
}
}
while(dep!=z[dep].poz)
{
s.push(z[dep].nr);
dep=z[dep].poz;
}
s.push(z[dep].nr);
fout<<maxi2<<'\n';
while(!s.empty())
{
fout<<s.top()<<' ';
s.pop();
}
return 0;
}