Pagini recente » Cod sursa (job #2409657) | Cod sursa (job #2409644)
#include <bits/stdc++.h>
using namespace std;
const int DIM = 100005;
int arr[DIM], aux[DIM], fth[DIM], stk[DIM];
int main(void) {
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
int n; cin >> n;
int k = 0;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
if (arr[i] > arr[stk[k]]) {
stk[++k] = i; fth[i] = stk[k - 1]; }
else {
int le = 1, ri = k;
while (le <= ri) {
int md = (le + ri) >> 1;
if (arr[stk[md]] < arr[i]) {
le = md + 1; }
else {
ri = md - 1; } }
stk[le] = i; fth[i] = stk[ri]; } }
cout << k << endl;
for (int i = stk[k], j = k; i; i = fth[i], --j) {
aux[j] = arr[i]; }
for (int i = 1; i <= k; ++i) {
cout << aux[i] << " "; }
return 0; }