Cod sursa(job #2673948)

Utilizator PetyAlexandru Peticaru Pety Data 18 noiembrie 2020 11:42:21
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

int n, a[100002], b[100002], nr, aib[100002], ind[100002], dp[100002], Norm[100002], ant[100002], y;
map<int, int>mp;

void update (int x, int val, int j) {
  for (int i = x; i <= nr; i += (i & -i)) {
    if (aib[i] < val) {
      aib[i] = val;
      ind[i] = j;
    }
  }
}

int query (int x, int &poz) {
  int ans = 0;
  for (int i = x; i; i -= (i & -i)) {
    if (ans < aib[i]) {
      ans = aib[i];
      poz = ind[i];
    }
  }
  return ans;
}


int main()
{
  fin >> n;
  for (int i = 1; i <= n; i++) {
    fin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    if (i == 1 || b[i] != b[i - 1]) {
      mp[b[i]] = ++nr;
      Norm[nr] = b[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i] = mp[a[i]];
    int last;
    dp[i] = 1 + query(a[i] - 1, last);
    ant[i] = last;
    update(a[i], dp[i], i);
  }
  int mx = query(nr, y);
  fout << mx << "\n";
  for (int i = 1; i <= n; i++) {
    if (dp[i] == mx) {
      int poz = i;
      vector<int>ans;
      while (poz) {
        ans.push_back(Norm[a[poz]]);
        poz = ant[poz];
      }
      reverse(ans.begin(), ans.end());
      for (auto it : ans)
        fout << it << " ";
      return 0;
    }
  }
  return 0;
}