Pagini recente » Cod sursa (job #2561965) | Cod sursa (job #2651424) | Cod sursa (job #2093699) | Cod sursa (job #389684) | Cod sursa (job #2936913)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, cnt;
int v[100005], lis[100005], poz[100005];
stack <int> solutie;
int CB(int x)
{
int st = 1, dr = cnt, mij, sol;
while(st <= dr)
{
mij = (st + dr) / 2;
if(x > lis[mij])
{
st = mij + 1;
}
else
{
dr = mij - 1;
sol = mij;
}
}
return sol;
}
int main()
{
fin >> n;
for(int i = 1; i <= n; i ++)
{
fin >> v[i];
}
poz[1] = 1;
lis[1] = v[1];
cnt = 1;
for(int i = 2; i <= n; i ++)
{
if(v[i] > lis[cnt])
{
lis[++cnt] = v[i];
poz[i] = cnt;
}
else
{
int aux = CB(v[i]);
lis[aux] = v[i];
poz[i] = aux;
}
}
fout << cnt << "\n";
for(int i = n; i >= 1; i --)
{
if(poz[i] == cnt)
{
solutie.push(v[i]);
cnt--;
}
}
while(!solutie.empty())
{
fout << solutie.top() << " ";
solutie.pop();
}
return 0;
}