Pagini recente » Cod sursa (job #2761627) | Cod sursa (job #2560854) | Cod sursa (job #39413) | Cod sursa (job #3158195) | Cod sursa (job #2111293)
#include<fstream>
#include<stack>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
const int inf = 2147483647;
const int Nmax = 100005;
int v[Nmax], poz[Nmax], t[Nmax], n, lung_max;
void citire(){
f >> n;
for(int i = 1; i <= n; i++)
f >> v[i];
}
void cautare(int x, int i){
lung_max++;
poz[lung_max] = inf;
int p, q, m;
p = 1;
q = lung_max;
while(p <= q){
m = (p + q) / 2;
if(x > poz[m])
p = m + 1;
else
if(x < poz[m])
q = m - 1;
else{
p = q = m;
}
}
poz[p] = x;
if(poz[lung_max] == inf){
lung_max--;
t[i] = p;
}
else
t[i] = lung_max;
}
void rezolvare(){
lung_max++;
poz[1] = v[1];
t[1] = 1;
for(int i = 2; i <= n; i++)
cautare(v[i], i);
}
void afisare(){
stack<int> s;
g << lung_max << '\n';
int i;
for(i = n; i >= 1; i--)
if(t[i] == lung_max){
lung_max--;
s.push(v[i]);
}
while(!s.empty()){
g << s.top() << ' ';
s.pop();
}
}
int main(){
citire();
rezolvare();
afisare();
return 0;
}