Cod sursa(job #2977787)

Utilizator Luca_Miscocilucainfoarena Luca_Miscoci Data 12 februarie 2023 14:02:20
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>

using namespace std;

const int nmax = 1e5;
int v[nmax + 5];
int s[nmax + 5];
int prevs[nmax + 5];

int bs(int e, int st, int dr) {
  int ind, step;
  ind = st - 1;
  step = 1 << 16;
  while (step){
    if (ind + step <= dr && v[s[ind + step]] < e)
      ind += step;
    step >>= 1;
  }
  return ind;
}

ofstream fout ("scmax.out");
void write(int poz) {
  if (poz != -1) {
    write(prevs[poz]);
    fout << v[poz] << " ";
  }
}

int main(){

  ifstream fin ("scmax.in");

  int n, l;

  fin >> n;
  for (int i = 0; i < n; ++i)
    fin >> v[i];

  int maxl = 0;
  for (int i = 0; i < n; i++){

    l = bs(v[i], 0, maxl - 1) + 1;
    s[l] = i;

    if (l > 0) prevs[i] = s[l - 1];

    else prevs[i] = -1;

    maxl = max (maxl, l + 1);
  }

  fout << maxl << "\n";
  write(s[maxl - 1]);

  return 0;
}