Pagini recente » Cod sursa (job #708882) | Cod sursa (job #282127) | Cod sursa (job #3268257) | Cod sursa (job #3031074) | Cod sursa (job #751216)
Cod sursa(job #751216)
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
using namespace std;
const int N_MAX=100011;
vector<int> v, w;
vector<int>::iterator it, iend;
int aib[N_MAX], f[N_MAX], l[N_MAX], index[N_MAX];
void Update(const int& N, int xIndex, int xValue)
{
for(; xIndex <= N; xIndex+=(xIndex & -xIndex))
if(l[aib[xIndex]] < l[xValue])
aib[xIndex]=xValue;
}
int Query(int xIndex)
{
int maxIndex=0;
for(; xIndex; xIndex-=(xIndex & -xIndex))
if(l[aib[xIndex]] > l[maxIndex])
maxIndex=aib[xIndex];
return maxIndex;
}
inline void Output(ostream& out, int x)
{
if(0 == x)
return;
Output(out, f[x]);
out<<v[x-1]<<' ';
}
int main()
{
int N, i, maxIndex=0;
ifstream in("scmax.in");
ofstream out("scmax.out");
in>>N;
copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v));
w=v;
sort(w.begin(), w.end());
it=w.begin(), iend=w.end();
iend=unique(it, iend);
for(i=1; i <= N; ++i)
index[i]=lower_bound(it, iend, v[i-1]) - it + 1;
for(i=1; i <= N; ++i)
{
f[i]=Query(index[i]-1);
l[i]=1+l[f[i]];
if(l[i] > l[maxIndex])
maxIndex=i;
Update(N, index[i], i);
}
out<<l[maxIndex]<<'\n';
Output(out, maxIndex);
return EXIT_SUCCESS;
}