Pagini recente » Cod sursa (job #1082005) | Cod sursa (job #570772) | Cod sursa (job #2669967) | Cod sursa (job #1874655) | Cod sursa (job #2576368)
#include <fstream>
#include <iostream>
#include <stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,a[100005],dp[100005],lungime=1,p[100005];
stack<int>stiva;
int cb(int x)
{
int st=1,dr=lungime;
while (st<=dr)
{
int mij=(st+dr)/2;
if (dp[mij]>=x)
{
dr=mij-1;
}
else
{
st=mij+1;
}
}
if (st==lungime+1)
return -1;
return st;
}
void afisare( )
{
for(int i=n;i>=1;i--)
{
if((p[i]==lungime && stiva.top()>a[i] )|| stiva.empty())
{
stiva.push(a[i]);
lungime--;
}
}
while (!stiva.empty()) {
fout<<stiva.top()<<" ";
stiva.pop();
}
}
int main( ) {
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>a[i];
int poz=cb(a[i]);
if(poz==-1)
{
dp[lungime]=a[i];
lungime++;
p[i]=lungime;
}
else
{
dp[poz]=a[i];
p[i]=poz;
}
}
lungime--;
fout<<lungime<<"\n";
afisare();
return 0;
}