Cod sursa(job #2279091)

Utilizator palomaPaloma Josse paloma Data 8 noiembrie 2018 21:51:29
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fi("scmax.in");
ofstream fo("scmax.out");

const int maxn = 1e5;

int n, v[maxn + 1], pos[maxn + 1], lmax, d[maxn + 1], lg[maxn + 1], str[maxn + 1];

int binarySearch(int idx) {
  int ans = 0;
  for (int step = (1<<lg[lmax]); step >= 1; step >>= 1) {
    if (ans + step <= lmax && pos[ans + step] < v[idx])
      ans += step;
  }
  return ans + 1;
}

int main()
{
  fi >> n;
  for (int i = 1; i <= n; i++)
    fi >> v[ i ];

  for (int i = 2; i <= n; i++)
    lg[ i ] = lg[i/2] + 1;

  pos[ 1 ] = v[ 1 ];
  d[ 1 ] = lmax = 1;
  for (int i = 2; i <= n; i++) {
    d[ i ] = binarySearch(i);
    lmax = max(lmax, d[ i ]);
    if (pos[d[ i ]] == 0)
      pos[d[ i ]] = v[ i ];
    else
      pos[d[ i ]] = min(pos[d[ i ]], v[ i ]);
  }

  fo << lmax << '\n';

  int k = lmax, crt = pos[lmax] ;
  str[ k-- ] = crt;
  for (int i = n; i >= 1; i--) {
    if (v[ i ] < crt && d[ i ] == k) {
      str[ k-- ] = v[ i ];
      crt = v[ i ];
    }
  }

  for (int i = 1; i <= lmax; i++) {
    fo << str[ i ] << ' ';
  }

  fo.close();
  fi.close();

  return 0;
}