Cod sursa(job #2989991)

Utilizator AndreiTitus10Andrei Titus AndreiTitus10 Data 7 martie 2023 13:08:48
Problema Subsir crescator maximal Scor 15
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fi=fopen("scmax.in","r");
    FILE*fo=fopen("scmax.out","w");
    int n , a[1002] , L[1002] , P[1002] , p , l , pmax , lmax;
    fscanf(fi,"%d",&n);
    for(int i = 1 ; i <= n ; i++)
        fscanf(fi,"%d",&a[i]);
    for(int i = n ; i >= 1 ; i--)
    {
        p = i;
        l = 0;
        for(int j = i + 1 ; j <= n ; j++)
            if(a[i] <= a[j] && L[j] > l)
            {
                l = L[j];
                p = j;
            }
        L[i] = l + 1;
        P[i] = p;
        if(L[i] > lmax)
        {
            lmax = L[i];
            pmax = i;
        }
    }

    fprintf(fo,"%d\n",lmax);
    p = pmax;

    while(p != P[p])
    {
        fprintf(fo,"%d\n",p);
        p = P[p];
    }
    fprintf(fo,"%d\n",p);
    fclose(fi);
    fclose(fo);
    return 0;
    ///Sa se determine un subsir al lui a care este ordonat strict crescator si care are lungimea maxima.
    ///5
    ///24 12 15 15 19
    ///3
    ///12 15 19
    /// 100p infoarena, limita de timp depasita
}