Pagini recente » Borderou de evaluare (job #1343180) | Cod sursa (job #1752286) | Borderou de evaluare (job #2666581) | Borderou de evaluare (job #2666573) | Cod sursa (job #1752880)
#include <iostream>
using namespace std;
int a[100001],smax[100001],pos[100001],n;
int smallestElementGreaterThan(int x){
int st = 1, dr = smax[0], gasit = -1;
while ( st <= dr ){
int m = (st+dr)>>1;
if ( smax[m] >= x ) {
gasit = m;
dr = m - 1;
}
else {
st = m + 1;
}
}
//cout<<gasit<<" "<<st<<" "<<dr<<" "<<x<<endl;
return gasit;
}
int main() {
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
int poz = smallestElementGreaterThan(a[i]);
if (poz == -1) {
smax[0]++;
smax[smax[0]] = a[i];
pos[i] = smax[0];
}
else{
smax[poz] = a[i];
pos[i] = poz;
}
}
cout<<smax[0]<<endl;
int poz=n;
for(int i=smax[0];i>0;i--){
while(pos[poz]!=i)poz--;
smax[i]=a[poz];
}
for(int i=1;i<=smax[0];i++){
cout<<smax[i]<<" ";
}
}