Pagini recente » Cod sursa (job #1921954) | Cod sursa (job #2885176) | Cod sursa (job #172054) | Cod sursa (job #1056968) | Cod sursa (job #1248760)
#include <iostream>
#include<fstream>
using namespace std;
int n, a[100001], b[100001], c[100001];
int gasit[100001], nr;
ofstream g("scmax.out");
void citeste()
{
int i;
ifstream f("scmax.in");
f>>n;
for (i=1;i<=n;i++)
f>>a[i];
}
void afisare(int i)
{
if (gasit[i]==0) g<<a[i]<<' ';
else
{
afisare(gasit[i]);
g<<a[i]<<' ';
}
}
void solutie1()
{
int i, j, maxim=1,pmax;
b[1]=1;
gasit[1]=0;
for (i=2;i<=n;i++)
{
j=i-1;
while (a[i]<a[j] && j>0) j--;
if (j==0) {b[i]=1; gasit[i]=0;}
else {b[i]=b[j]+1; if (b[i]>maxim) {maxim=b[i]; pmax=i;} gasit[i]=j;}
}
g<<pmax<<'\n';
afisare(pmax);
}
int cautare(int i, int st, int dr)
{
int poz=1, pas=1<<21;
while (pas>>=1)
{
if (poz+pas<=nr && a[poz+pas]<=a[i]) poz+=pas;
}
if (a[poz]==a[i]) return poz;
if(poz==nr) {nr++; poz++;}
return poz;
}
void solutie2()
{
int i, j=1;
c[1]=1;
gasit[1]=0;
nr=1;
for (i=2;i<=n;i++)
{
j=cautare(i, 1, nr);
gasit[i]=c[j-1];
c[j]=i;
}
g<<nr<<'\n';
afisare(nr);
}
int main()
{
citeste();
//solutie1();
solutie2();
}