Pagini recente » Cod sursa (job #1945867) | Cod sursa (job #80913) | Cod sursa (job #1105971) | Cod sursa (job #2842870) | Cod sursa (job #480534)
Cod sursa(job #480534)
#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;
long x;
vector<long> v,smax,bst,pre;
vector<long>::iterator it;
fin>>n;
pre.resize(n);
smax.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)
{
it=smax.begin();
int j=0;
while(it!=smax.end() && v[*it]<v[i])
{
j++;
it++;
}
smax[j]=i;
bst[i]=j;
if(j>0)
pre[i]=smax[j-1];
else
pre[i]=-1;
}
//cout<<"\n";
int max=0,pos;
for(int i=0; i<n; ++i)
{
if(bst[i]>max)
{
max=bst[i];
pos=i;
}
}
fout<<max+1<<"\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;
}