Pagini recente » Cod sursa (job #332609) | Cod sursa (job #2200921) | Cod sursa (job #2610386) | Cod sursa (job #1449155) | Cod sursa (job #2579972)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, v[100005], lungime[100005], lgmax, pre[100005];
void citire() {
fin >> n;
for(int i = 1; i <= n; i++)
fin >> v[i];
}
void solve() {
for(int i = 1; i <= n; i++) {
int st = 1, dr = lgmax, found;
if(v[lungime[lgmax]] < v[i]) found = lgmax+1;
while(st <= dr) {
int m = (st+dr)/2;
if(v[lungime[m]] >= v[i] && v[lungime[m-1]] < v[i]) {
found = m;
break;
} else if(v[lungime[m]] >= v[i])
dr = m-1;
else
st = m+1;
}
lungime[found] = i;
pre[i] = lungime[found-1];
lgmax = max(lgmax, found);
}
fout << lgmax << '\n';
}
void afis(int x) {
if(!x) return;
afis(pre[x]);
fout << v[x] << ' ';
}
int main() {
citire();
solve();
afis(lungime[lgmax]);
}