Cod sursa(job #2610915)

Utilizator andreihaivas006Daniel Haivas andreihaivas006 Data 5 mai 2020 21:18:52
Problema Subsir crescator maximal Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
/*
    dp[i] = lungimea subsirului maximal crescator din primele i elemente(INCLUSIV ELEMENTUL i)
    dp[i] = 1 + max(dp[j])   , j = 1 .. i
              s[i] > s[j]
    solutie: max(dp[])
*/
#include <iostream>
#include <stdio.h>
#include <fstream>

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

int n, s[100000];
int dp[100000];

int scmax(int i)
{
    if (dp[i] != 0)
        return dp[i];
    if (i == 1)
        return dp[1] = 0;

    int max = dp[1];
    for (int j = 1; j < i; j++)
        if (scmax(j) > max && s[i] > s[j] )
            max = scmax(j);
    return dp[i] = 1 + max;
}

void scmax_()
{
    dp[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        int max = dp[1];
        for (int j = 1; j < i; j++)
            if (s[i] > s[j] && dp[j] > max)
                max = dp[j];
        dp[i] = max + 1;
    }
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> s[i];
    scmax(n);
    int max = dp[1];
    for (int i = 1; i <= n; i++)
        if (dp[i] > max)
            max = dp[i];
    fout << max;
    return 0;
}