Cod sursa(job #2892048)

Utilizator avtobusAvtobus avtobus Data 20 aprilie 2022 16:44:51
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <algorithm>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main(void) {
  freopen("scmax.in", "r", stdin);
  freopen("scmax.out", "w", stdout);
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> a(N), prev(N, -1);
  for(int i = 0; i < N; i++) {
    cin >> a[i];
  }

  vector<int> best {a[0]}, ind{0};
  for(int i = 1; i < N; i++) {
    auto it = lower_bound(best.begin(), best.end(), a[i]);
    if (it == best.end()) {
      if (!best.empty()) {
        prev[i] = ind.back();
      }
      best.push_back(a[i]);
      ind.push_back(i);
    } else {
      int k = it - best.begin();
      best[k] = a[i];
      ind[k] = i;
      if (k > 0) {
        prev[i] = ind[k-1];
      }
    }
  }
  cout << best.size() << endl;
  // for(auto x: best) { cout << x << ' '; } cout << "\n";
  vector<int> res;
  for(int i = ind.back(); i != -1; i = prev[i]) {
    res.push_back(a[i]);
  }
  reverse(res.begin(), res.end());
  for(const auto& x: res) { cout << x << ' ';} cout << "\n";
  return 0;
}