Pagini recente » Cod sursa (job #801760) | Cod sursa (job #903868) | Cod sursa (job #2038530) | Cod sursa (job #982698) | Cod sursa (job #2637076)
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, v[100005], lungime[100005], pre[100005], lgmax;
void citire() {
fin >> n;
for(int i = 1; i<= n; i++)
fin >> v[i];
}
void solve() {
for(int i = 1; i <= n; i++) {
int found = 0;
if(v[lungime[lgmax]] < v[i]) found = lgmax+1;
int st = 1, dr = lgmax;
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);
}
}
void afis(int pos) {
if(!pos) return;
afis(pre[pos]);
fout << v[pos] << ' ';
}
void print() {
fout << lgmax << '\n';
afis(lungime[lgmax]);
}
int main() {
citire();
solve();
print();
}