Pagini recente » Cod sursa (job #727947) | Cod sursa (job #2469602) | Cod sursa (job #2030529) | Cod sursa (job #979801) | Cod sursa (job #1207529)
using namespace std;
#include <fstream>
#include <algorithm>
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int Nmax = 100010;
int n, p;
int v[Nmax], poz[Nmax], lst[Nmax], D[Nmax], AIB[Nmax], pred[Nmax];
char s[1200000];
int query(int) ;
void update(int, int) ;
int get_int() ;
int main()
{
int i, h, bst = 0;
fin >> n; fin.get(); fin.getline(s, 1200000);
for(i = 1; i <= n; ++i)
{
v[i] = get_int();
lst[i] = v[i];
}
sort(lst + 1, lst + n + 1);
for(i = h = 1; i <= n; ++h)
{
lst[h] = lst[i];
while(lst[i] == lst[h]) ++i;
}
for(i = 1; i <= n; ++i)
poz[i] = lower_bound(lst + 1, lst + h, v[i]) - lst;
for(i = 1; i <= n; ++i)
{
pred[i] = query(poz[i] - 1);
D[i] = 1 + D[pred[i]];
if(D[i] > D[bst]) bst = i;
update(i, poz[i]);
}
fout << D[bst] << '\n';
for(h = 0; bst > 0; bst = pred[bst])
lst[h++] = bst;
for(--h; h >= 0; --h)
fout << v[lst[h]] << ' ';
fout << '\n';
return 0;
}
int query(int poz)
{
int el = 0;
for(; poz; poz &= poz - 1)
{
if(D[el] < D[AIB[poz]])
el = AIB[poz];
}
return el;
}
void update(int ind, int x)
{
while(x <= n)
{
if(D[AIB[x]] < D[ind])
AIB[x] = ind;
else return;
x += x & -x;
}
}
int get_int()
{
int x = 0;
while('0' <= s[p] && s[p] <= '9')
x = x * 10 + s[p] - '0', ++p;
++p;
return x;
}