Cod sursa(job #1999116)

Utilizator ruxandramateiMatei Ruxandra ruxandramatei Data 10 iulie 2017 12:52:26
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int v[100001], n;//n - array dat, cu dimensiunea n
int pozitie[100001], lgPoz;//array cu pozitiile ordonate in functie de val;
int tata[100001];//array care va memora legaturile parinte
int lgSecv[100001];

int cautInPoz(int temp){//fct. cautare elem temp, in v;
  int st = 1;
  int dr = lgPoz;
  int mijloc;

  while(st <= dr){
    mijloc = (st + dr) / 2;
    if(v[pozitie[mijloc]] == temp)
      return mijloc;
    if(v[pozitie[mijloc]] > temp)
      dr = mijloc - 1;
    else
      st = mijloc + 1;
  }
  return st;//in cazul in care nu e gasit
}

void construireP(){
  lgPoz = 1;
  lgSecv[1] = 1;
  pozitie[1] = 1;
  for(int i = 2; i <= n; i ++){
    int pozInserat = cautInPoz(v[i]);
    tata[i] = pozitie[pozInserat - 1];
    //tatal elementului v[i] aflat pe poz i este elementul precedent aflat pe poz[pozIns - 1];
    lgSecv[i] = pozInserat;//secventa pana in punctul i este poz de Inserat
    pozitie[pozInserat] = i;//actualizam array-ul cu poz
    if(lgPoz < pozInserat)//daca avem o lg mai mare actualizam
      lgPoz = pozInserat;
  }
}

void rezolvare(){
  int plec = 0;
  int lgmaxima = 0;
  for(int i = 1; i <= n; i++)
    if(lgSecv[i] > lgmaxima){
      lgmaxima = lgSecv[i];
      plec = i;
    }
  out << lgmaxima << '\n';
  for(int i = plec ; i !=0; i = tata[i])
    out << v[i] << ' ';
}

void citire(){
  in >> n;
  for(int i = 1; i <= n; i++)
    in >> v[i];
}

int main(){
  citire();
  construireP();
  rezolvare();
  return 0;
}