Pagini recente » Cod sursa (job #1954782) | Cod sursa (job #2731938) | Cod sursa (job #3176914) | Cod sursa (job #1087447) | Cod sursa (job #221867)
Cod sursa(job #221867)
#include<fstream>
using namespace std;
const int m=100005;
int n,v[m],lung[m],pred[m];
ofstream out("scmax.out");
void citire ()
{
ifstream in ("scmax.in");
in>>n;
for(int i=1;i<=n;i++)
in>>v[i];
}
void lucru ()
{
int mmax;
lung[1]=1;
pred[1]=0;
for(int i=2;i<=n;i++)
{
mmax=0;
for(int j=1;j<i;j++)
{
if(v[i]>v[j]&&lung[j]>mmax)
{
pred[i]=j;
mmax=lung[j];
}
}
lung[i]=mmax+1;
}
}
int pozmax()//returneaza poz pe care se afla cea mai mare val din vectorul lung
{
int i,pmax=1;
for(i=1;i<=n;i++)
if(lung[i]>lung[pmax])
pmax=i;
return pmax;
}
void recuperare(int x)
{
if(x==0)
return;
recuperare(pred[x]);
out<<v[x]<<" ";
}
int main()
{
citire ();
lucru ();
int poz=pozmax();
out<<lung[poz]<<"\n";
recuperare(poz);
out.close();
return 0;
}