Cod sursa(job #1419830)

Utilizator andreiagAndrei Galusca andreiag Data 16 aprilie 2015 19:35:39
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
const int Nmax = 100555;

int a[Nmax];
int b[Nmax];
int dp[Nmax];

int ret[Nmax];

int main() {

  ifstream f ("scmax.in");
  ofstream g ("scmax.out");
  int N, x, len = 0;

  f >> N;
  
  for (int i = 0; i < N; i++) {
    f >> x;
    a[i] = x;
    int pos = len+1;
    while (pos > 1 && x <= dp[pos-1])
      --pos;
    dp[pos] = x;
    b[i] = pos;
    if (pos > len)
      len = pos;
  }
  g << len << '\n';

  x = len;
  for (int i = N-1; i >= 0; i--) {
    if (b[i] == x) {
      --x;
      ret[x] = a[i];
    }
  }
  for (int i = 0; i < len; i++)
    g << ret[i] << ' ';
  g << '\n';

  return 0;
}