Pagini recente » Cod sursa (job #741513) | Cod sursa (job #575026) | Cod sursa (job #1628808) | Cod sursa (job #1937480) | Cod sursa (job #2267124)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int a[1000],best[1000],poz[1000],lgmax,s[1000];
int n,i;
int cautbin(int x) //cauta binar in best cel mai mic element > ca x si returneaza pozitia lui
{
int st=0,mijloc=0;
int dr=lgmax+1;
while(dr-st>1)
{
mijloc=(st+dr)/2;
if(best[mijloc]>=x)
{
dr=mijloc;
}
else st=mijloc;
}
return dr;
}
void citire()
{
fin>>n;
for(i=1; i<=n; i++)
fin>>a[i];
}
void constructiebest()
{
int i,pozitie;
best[1]=a[1];
lgmax=1;
poz[1]=1;
for(i=2; i<=n; i++)
{
if(a[i]>best[lgmax])
{
best[++lgmax]=a[i];
poz[i]=lgmax;
}
else
{
pozitie=cautbin(a[i]);
best[pozitie]=a[i];
poz[i]=pozitie;
}
}
}
void afisare()
{
int cine;
fout<<lgmax<<'\n';
cine=lgmax;
for(i=n; i>=1; i--)
{
if(poz[i]==cine)
{
s[cine]=a[i];
cine--;
}
}
for(i=1; i<=lgmax; i++)
fout<<s[i]<<' ';
}
int main()
{
citire();
constructiebest();
afisare();
return 0;
}