Cod sursa(job #2626969)

Utilizator lucidanescu28Danescu Lucian lucidanescu28 Data 9 iunie 2020 11:49:58
Problema Subsir crescator maximal Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>
#include <stdlib.h>

int main(){
    int n, a[100000], max, start;

    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    
    int *D = malloc(n * sizeof(int));

    for(int i = 0; i < n; i++){
        max = 0;
        for(int j = 0; j < i; j++)
            if(a[j] < a[i] && D[j] > max) max = D[j];
        
        D[i] = max + 1;
    }

    max = 0;
    for(int i = 0; i < n; i++)
        if(D[i] > max){
            max = D[i];
            start = i;
        }

    int size = max;
    int *ans = malloc(max * sizeof(int));
    ans[--max] = a[start];

    while(max){
        for(int i = 0; i < start; i++)
            if(D[i] == D[start] - 1 && a[i] <= a[start]){
                ans[--max] = a[i];
                start = i;
                break;
            }
    }

    for(int i = 0; i < size; i++)
        printf("%d ", ans[i]);

    
}