Pagini recente » Cod sursa (job #1291687) | Cod sursa (job #1001379) | Cod sursa (job #2085286) | Cod sursa (job #362269) | Cod sursa (job #1149714)
#include <fstream>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int a[100001],poz[100001],cmax,i,n,ca,cb;
int x[100001];
int best(int st,int dr)
{
if (st>dr)
return 1;
if (st==dr && x[poz[st]]>x[i])
return st+1;
if (st==dr && x[poz[st]]==x[i])
return 0;
int pivot=(st+dr)/2;
if (x[poz[pivot]]<=x[i])
return best(st,pivot);
return best(pivot+1,dr);
}
int main()
{
f>>n;
for (i=1;i<=n;i++)
f>>x[i];
for(i=n;i>0;i--)
{
ca=x[i];
int aux;
if (x[i]>x[poz[1]])
aux=1;
else aux=best(1,cmax);
poz[aux]=i;
a[i]=poz[aux-1];
cmax=aux>cmax?aux:cmax;
}
cb=poz[cmax];
g<<cmax<<'\n';
while (cb)
{
g<<x[cb]<<' ';
cb=a[cb];
}
g<<'\n';
g.close();
return 0;
}