Cod sursa(job #1049853)

Utilizator tvararuVararu Theodor tvararu Data 7 decembrie 2013 21:12:32
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void show (vector<int> &v) {
  for (int i = 0, len = v.size(); i < len; i++) {
    cout << v[i] << ' ';
  }
  cout << '\n';
}

int main (int argc, char const *argv[])
{
  ifstream in("scmax.in");
  int N = 0;
  in >> N;
  
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    in >> v[i];
  }
  in.close();
  
  int max = -0x7fffffff, maxIndex = 0;
  vector<int> len(N, 1), poz(N, -1);
  for (int i = N - 2; i >= 0; i--) {
    for (int j = i + 1; j < N; j++) {
      if (v[i] < v[j] && len[i] <= len[j]) {
        len[i] = len[j] + 1;
        poz[i] = j;
        
        if (max < len[i]) {
          max = len[i];
          maxIndex = i;
        }
      }
    }
  }
  
  // show(v);
  // show(len);
  // show(poz);
  
  ofstream out("scmax.out");
  out << max << endl;
  for (int i = maxIndex; i != -1; i = poz[i]) {
    out << v[i] << ' ';
  }
  out << endl;
  
  return 0;
}