Cod sursa(job #2032953)

Utilizator preda.andreiPreda Andrei preda.andrei Data 5 octombrie 2017 21:53:03
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>

using namespace std;

struct ListTop
{
  int len, index;
};

int FindIndex(const vector<ListTop> &lists, const vector<int> &vec, int val)
{
  int pos = -1;
  int power = (1 << 30);

  while (power > 0) {
    if (pos + power < (int)lists.size() &&
              val <= vec[lists[pos + power].index]) {
      pos += power;
    }
    power >>= 1;
  }

  if (pos + 1 >= (int)lists.size()) {
    return -1;
  }
  return pos + 1;
}

void Print(const vector<int> &vec, const vector<int> &pred, int ind, ofstream &fout)
{
  if (pred[ind] != -1) {
    Print(vec, pred, pred[ind], fout);
  }
  fout << vec[ind] << " ";
}

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

  int n;
  fin >> n;

  vector<int> vec(n);
  vector<int> pred(n, -1);

  vector<ListTop> lists;

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

    auto index = FindIndex(lists, vec, vec[i]);
    if (index == -1) {
      lists.push_back({ 1, i });
    } else {
      lists[index].len += 1;
      pred[i] = lists[index].index;
      lists[index].index = i;
    }
  }

  int res_ind = 0;
  for (size_t i = 1; i < lists.size(); ++i) {
    if (lists[i].len > lists[res_ind].len) {
      res_ind = i;
    }
  }

  fout << lists[res_ind].len << "\n";
  Print(vec, pred, lists[res_ind].index, fout);

  return 0;
}