Cod sursa(job #2610919)

Utilizator andreihaivas006Daniel Haivas andreihaivas006 Data 5 mai 2020 21:26:38
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 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 = 0;
    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] = 0;
    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_();
    int max = dp[1];
    for (int i = 1; i <= n; i++)
    {
        cout << dp[i] << " ";
        if (dp[i] > max)
            max = dp[i];
    }
    g << max;
    //cout << "\n\n" << max;
    return 0;
}