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