Pagini recente » Istoria paginii utilizator/anamaria.dumitrahe | Cod sursa (job #867564) | Profil enicadia | Rating Perju Smaranda (smaranda_perju) | Cod sursa (job #2553668)
#include <fstream>
#define NMAX 100002
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int a[NMAX], lgbest;
int best[NMAX];
int poz[NMAX];
int sol[NMAX];
int n;
int cautbin(int x);
int main()
{
int i, j, pozai;
fin>>n;
for (i=1; i<=n; i++) fin>>a[i];
best[1]=a[1]; poz[1]=1; lgbest=1;
for (i=2; i<=n; i++)
if(a[i]>best[lgbest]) {best[++lgbest]=a[i]; poz[i]=lgbest;}
else
{
pozai=cautbin(a[i]);
best[pozai]=a[i];
poz[i]=pozai;
}
fout<<lgbest<<'\n';
for (j=n,i=lgbest; i>0; j--)
if(poz[j]==i) {sol[i]=a[j]; i--;}
for (i=1; i<=lgbest; i++)
fout<<sol[i]<<' ';
fout.close();
return 0;
}
int cautbin(int x)
{
int st=0, dr=lgbest+1, mij;
while (dr-st>1)
{
mij=(st+dr)/2;
if (a[mij]<x) st=mij;
else dr=mij;
}
return dr;
}