Cod sursa(job #2413804)

Utilizator minculescualex9Minculescu Alex minculescualex9 Data 23 aprilie 2019 18:42:58
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.98 kb
/*
#include    <stdio.h>

FILE*f=fopen("scmax.in","r");
FILE*g=fopen("scmax.out","w");

int best[100003],a[100003], max,sol=0,poz[100003],p;
int n;


void read()
  {
    fscanf(f,"%d",&n);
    for(int i=1;i<=n;++i)
        fscanf(f,"%d",&a[i]);
  }


void dinamic()
  {
    int i,j;

    best[n] = 1;
    poz[n] = -1;
    max = 1;
    p = n;

    for(i = n-1; i >= 1; --i)
    {
        best[i] = 1;
        poz[i] = -1;

        for(j = i+1; j <= n; ++j)
            if(a[i] < a[j] && best[i] < best[j]+1)
            {
                best[i] = best[j]+1;
                poz[i] = j;

                if(best[i] > max){
                    max = best[i];
                    p = i;
                }
            }
    }
  }


void constr(){
    int i;
    i = p;

    while(i != -1){
        fprintf(g,"%d ",a[i]);
        i = poz[i];
    }
}


int main()
  {
  read();

  dinamic();

  fprintf(g,"%d\n",max);

  constr();

  return 0;
  }
*/

#include <iostream>
#include <fstream>

using namespace std;

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

#define nMax 100005

int n, a[nMax], best[nMax], poz[nMax], Max, p;

void dinamic(){
    best[n] = 1;
    poz[n] = -1;

    Max = 1;
    p = n;

    for(int i = n - 1; i >= 1; --i){
        best[i] = 1;
        poz[i] = -1;

        for(int j = i+1; j <= n; ++j)
            if(a[i] < a[j] && best[i] < best[j]+1){
                best[i] = best[j]+1;
                poz[i] = j;

                if(best[i] > Max){
                    Max = best[i];
                    p = i;
                }
            }
    }

}

void constr(){
    int i = p;
    while(i != -1){
        fout << a[i] << " ";
        i = poz[i];
    }
}

int main(){

    //Citire
    fin >> n;
    for(int i = 1; i <= n; ++i)
        fin >> a[i];

    dinamic();

    fout << Max << "\n";
    constr();

    fin.close();
    fout.close();
    return 0;
}