Pagini recente » Cod sursa (job #3203748) | Cod sursa (job #1552611) | Cod sursa (job #1874778) | Cod sursa (job #3287207) | Cod sursa (job #2492427)
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
ifstream f ("scmax.in");
ofstream g ("scmax.out");
int n;
int a[NMAX] , pos[NMAX] , ans[NMAX] , aux[NMAX];
int Binary_Search(int val , int &k)
{
if(aux[k] < val)
{
aux[++k] = val;
return k;
}
int st = 1 , dr = k , mij = 0 , pos = 0;
while(st <= dr)
{
mij = (dr - st) / 2 + st;
if(aux[mij] >= val)
{
pos = mij;
dr = mij - 1;
}
else st = mij + 1;
}
aux[pos] = val;
return pos;
}
int main()
{
int i , k = 0;
f >> n;
for(i = 1 ; i <= n ; i++)
{
f >> a[i];
pos[i] = Binary_Search(a[i] , k);
}
int m = k;
for(i = n ; i >= 1 ; i--)
{
if(pos[i] == m)
ans[m--] = a[i];
}
g << k << '\n';
for(i = 1 ; i <= k ; i++)
g << ans[i] << ' ';
return 0;
}