Cod sursa(job #2213069)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 15 iunie 2018 16:43:22
Problema Subsir 2 Scor 42
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
using namespace std;
int N, ElementsList[5005], i, j, LastElement[5005], ElementCount[5005], mx, maxpoz, maxim;
int main()
{
    FILE *fin, *fout;
    fin=freopen("subsir2.in", "r", stdin);
    fout=freopen("subsir2.out", "w", stdout);
    scanf("%d", &N);
    for(i=1; i<=N; i++){
        scanf("%d", &ElementsList[i]); ElementCount[i]=1;
    }
    for(i=N; i>=1; i--){
        mx=0;
        for(j=i+1; j<=N; j++){
            if(ElementCount[i]>mx){mx=ElementCount[i]; maxpoz=i;}
            if(ElementsList[i]<ElementsList[j] && ElementCount[i]+ElementCount[j]>mx){
                LastElement[i]=j;
                mx=ElementCount[j]+ElementCount[i];
            }
        }
        if(ElementCount[i]<mx){
            ElementCount[i]=mx;
        }
        if(mx>maxim){maxim=mx; maxpoz=i;}
    }
    printf("%d%c", mx, '\n');
    do{
        printf("%d ", maxpoz);
        maxpoz=LastElement[maxpoz];
    }while(maxpoz);
    return 0;
}