Pagini recente » Cod sursa (job #1210077) | Cod sursa (job #2642194) | Cod sursa (job #2547206) | Cod sursa (job #287222) | Cod sursa (job #485522)
Cod sursa(job #485522)
#include<fstream>
#include<iostream>
#include<vector>
#include<stack>
#include<iterator>
using namespace std;
int findpos(vector<long>& v, vector<long>& scmax,unsigned int scmaxsize, long val)
{
/*int l=0,r=scmaxsize,index=(l+r)/2;
//cout<<l<<" - "<<r<<" with "<<val<<endl;
while(l<r)
{
index=(l+r)/2;
//cout<<l<<" "<<r<<" "<<index<<endl;
if(val>v[scmax[index]])
{
l=index+1;
}
else if(val<v[scmax[index]])
{
r=index;
}
else
return index;
}
return r;*/
unsigned int steps,i;
for(steps=1; steps<scmaxsize+1; steps<<=1) ;
for(i=0; steps; steps>>=1)
if(i+steps<=scmaxsize && val<=v[scmax[scmaxsize-i-steps]])
i+=steps;
return scmaxsize-i;
}
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]<<" ";
}
//cout<<endl;
for(int i=0; i<n; ++i)
{
int j=0;
/*while(j<scmaxsize && v[scmax[j]]<v[i])
{
j++;
}
*/
j=findpos(v, scmax, scmaxsize, v[i]);
if(j==scmaxsize)
scmaxsize++;
scmax[j]=i;
bst[i]=j+1;
if(j>0)
pre[i]=scmax[j-1];
else
pre[i]=-1;
/*cout<<j<<"\nscmax: "<<scmaxsize<<endl;
for(int l=0; l<scmaxsize; ++l)
{
cout<<v[scmax[l]]<<" ";
}
cout<<endl;*/
}
int max=0,pos=0;
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();
}
//cout<<"\n";
fin.close();
fout.close();
return 0;
}