Cod sursa(job #2346592)

Utilizator stratonedanielDaniel Stratone stratonedaniel Data 17 februarie 2019 20:38:17
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.5 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <queue>

typedef struct my_struct{

    int length;
    int how_you_got_there;
    struct my_struct *next;
    int index;

}MyArray;

typedef struct lengths{

    struct my_struct *pointer;

}Lenghts;


using namespace std;

void free_list(MyArray **head)
{
    MyArray *p=(*head);
    MyArray *save;

    while(p!=NULL)
    {
        save=p;
        p=p->next;
        free(save);

    }

}

void recursion(MyArray *possible_lengths,int *sequence,int maximum_length,int index,FILE* write)
{
    if(maximum_length==0)
        return;

    if(maximum_length!=1)
        recursion(possible_lengths,sequence,maximum_length-1,possible_lengths[index].how_you_got_there,write);
    else
        recursion(possible_lengths,sequence,maximum_length-1,index,write);

    fprintf(write,"%d ",sequence[index]);
}

int main()
{

    ifstream read("scmax.in");
    //ofstream write("scmax.out");

    FILE *write=fopen("scmax.out","w");

    int length_of_array;

    read>>length_of_array;

    int* sequence=(int*)calloc(length_of_array,sizeof(int));
    MyArray *possible_lengths=(MyArray*)calloc(length_of_array,sizeof(MyArray));
    MyArray *head;
    Lenghts *the_lengths=(Lenghts*)calloc(100001,sizeof(Lenghts));

    for(int i=0;i<length_of_array;i++)
    {
        possible_lengths[i].how_you_got_there=-1;
        read>>sequence[i];
    }

    possible_lengths[0].length=1;

    MyArray *one=(MyArray*)calloc(1,sizeof(MyArray));
    one->length=1;
    one->index=0;
    one->next=NULL;

    head=one;
    MyArray*new_node;

    int maximum_length=1;
    int index_of_maximum_length=-1;

    MyArray *p;
    MyArray *q;
    int ok;

    for(int i=1;i<length_of_array;i++)
    {
        ok=0;
        p=head;

        while(p!=NULL && sequence[p->index]>=sequence[i])
            p=p->next;


        if(p!=NULL)
        {
            possible_lengths[i].length=p->length+1;
            possible_lengths[i].how_you_got_there=p->index;

            new_node=(MyArray*)calloc(1,sizeof(MyArray));
            new_node->index=i;
            new_node->length=possible_lengths[i].length;

            if(possible_lengths[i].length>maximum_length)
            {
                maximum_length=possible_lengths[i].length;
                index_of_maximum_length=i;

                ok=1;

                the_lengths[possible_lengths[i].length].pointer=new_node;

                new_node->next=head;
                head=new_node;
            }



        }
        else if(p==NULL)
        {
            possible_lengths[i].length=1;
            new_node=(MyArray*)calloc(1,sizeof(MyArray));
            new_node->next=NULL;
            new_node->index=i;
            new_node->length=1;
            ok=1;
            one->next=new_node;

        }


        if(ok==0)
        {
            p=the_lengths[possible_lengths[i].length].pointer;

            new_node->next=p->next;
            p->next=new_node;

        }
    }


    fprintf(write,"%d\n",maximum_length);
        recursion(possible_lengths,sequence,maximum_length,index_of_maximum_length,write);

    //for(int i=0;i<length_of_array;i++)
      //  cout<<sequence[i]<<" "<<possible_lengths[i].length<<" "<<possible_lengths[i].how_you_got_there<<'\n';


    free(the_lengths);
    free_list(&head);
    free(sequence);
    free(possible_lengths);
    read.close();
    fclose(write);

    return 0;
}