Pagini recente » Cod sursa (job #3277051) | Cod sursa (job #1197495) | Cod sursa (job #2419867) | Cod sursa (job #1100958) | Cod sursa (job #2472154)
#include <iostream>
#include <fstream>
# define Nmax 100001
using namespace std;
int n,v[Nmax],a[Nmax],i,w[Nmax],x,lung,lungmax,j,poz,q[Nmax],ok;
ifstream f("scmax.in");
ofstream g("scmax.out");
int caut(int x)
{
int s=1,d=lung,poz=-1;
bool gasit=true;
while(s<=d)
{
int mid=(s+d)/2;
if(x==a[mid]) gasit=false;
if(x<a[mid])
{
d=mid-1;
poz=mid;
}
if(x>=a[mid])s=mid+1;
}
if(gasit==false)poz=0;
return poz;
}
int main()
{
f>>n;
for(i=1; i<=n; i++) {f>>v[i];if(v[i]!=v[i-1] and i>1)ok=1;}
if(ok==1){
lung=1;
a[1]=v[1];
for(i=2; i<=n; i++)
{
poz=caut(v[i]);
if(poz==-1)
{
lung++;
a[lung]=v[i];
q[i]=lung;
}
else if(poz!=0)
{
a[poz]=v[i];
q[i]=poz;
}
if(lung>lungmax)
lungmax=lung;
}
g<<lungmax<<'\n';lung=lungmax;i=n;
while(q[i]!=lung)i--;w[lung]=v[i];
for(i=n;i>=1;i--){
if(w[lung]>v[i] and v[i]>w[lung-1] and q[i]==lung)w[lung]=v[i];
if(q[i]==lung-1 and v[i]<w[lung]){
w[lung-1]=v[i];lung--;
} if(lung==0) break;
}for(i=1;i<=lungmax;i++) g<<w[i]<<" ";}else g<<1<<'\n'<<v[1];
}