Pagini recente » Cod sursa (job #2775527) | Cod sursa (job #2776928) | Cod sursa (job #2172558) | Cod sursa (job #2250215) | Cod sursa (job #480537)
Cod sursa(job #480537)
#include<fstream>
#include<iostream>
#include<vector>
#include<stack>
#include<iterator>
using namespace std;
int main()
{
fstream fin("scmax.in", fstream::in);
fstream fout("scmax.out", fstream::out);
int n,scmaxsize=0;
long x;
vector<long> v,scmax,bst,pre;
fin>>n;
pre.resize(n);
scmax.resize(n);
bst.resize(n);
for(int i=0; i<n; ++i)
{
fin>>x;
v.push_back(x);
//cout<<v[i]<<" ";
}
for(int i=0; i<n; ++i)
{
int j=0;
while(j<scmaxsize && v[scmax[j]]<v[i])
{
j++;
}
if(j==scmaxsize)
scmaxsize++;
scmax[j]=i;
bst[i]=j+1;
if(j>0)
pre[i]=scmax[j-1];
else
pre[i]=-1;
/*cout<<"scmax: "<<endl;
for(int l=0; l<scmaxsize; ++l)
{
cout<<v[scmax[l]]<<" ";
}
cout<<endl;*/
}
//cout<<"\n";
int max=0,pos;
for(int i=0; i<n; ++i)
{
if(bst[i]>max)
{
max=bst[i];
pos=i;
}
}
fout<<max<<"\n";
stack<long> st;
while(pos!=-1)
{
st.push(v[pos]);
pos=pre[pos];
}
while(!st.empty())
{
fout<<st.top()<<" ";
st.pop();
}
fout<<"\n";
fin.close();
fout.close();
return 0;
}