Pagini recente » Cod sursa (job #102454) | Cod sursa (job #2144904)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
const int Nmax = 100005;
int lis[Nmax] , top , poz[Nmax] , n , a[Nmax];
int main()
{
int stg , drp , mij , p , x , mx , j;
fin >> n;
fin >> x;
a[1] = x;
top = 1;
lis[top] = x;
poz[top] = 1;
for(int i = 2 ; i <= n ; i++)
{
fin >> x;
if(x <= lis[1])
{
lis[1] = x;
poz[i] = 1;
}
else if(x > lis[top])
{
++top;
lis[top] = x;
poz[i] = top;
}
else
{
stg = 1 , drp = top;
while(stg <= drp)
{
mij = (stg + drp) / 2;
if(lis[mij] >= x)
{
p = mij;
drp = mij - 1;
}
else stg = mij + 1;
}
lis[p] = x;
poz[i] = p;
}
a[i] = x;
}
fout << top << "\n";
mx = top;
for(j = n ; j >= 1 && mx != poz[j] ; j--)
;
top = 1;
lis[top] = a[j];
p = j;
mx--;
for(int i = p - 1 ; i >= 1 && mx > 0 ; i--)
if(poz[i] == mx && a[i] < a[p])
{
--mx;
p = i;
lis[++top] = a[i];
}
for(int i = top ; i >= 1 ; i--)
fout << lis[i] << " ";
fout << "\n";
fin.close();
fout.close();
return 0;
}