Cod sursa(job #823572)

Utilizator marS003Nastase Liviu marS003 Data 25 noiembrie 2012 12:06:44
Problema Subsir crescator maximal Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 2.04 kb
#include<stdio.h>
#include<stdlib.h>

void inc(int* a,int n){
    int c;
    for (c = 0 ; c < n ; c++){
        a[c] += 5;
        }
    }

void afisare(int *a,int n){
	FILE *out;
	out = fopen("scmax.out","w");
    int c;
    fprintf(out,"%d\n",n);
    for (c = 0 ; c < n ; c++){
        fprintf(out,"%d ",a[c]);
        }
       	
    }

int binarySearch(int *a,int x,int first, int last){
    if( first == last) {
        if (a[first] >= x) return first;
            else return (first + 1);
        }
        else{
            int mid = (first + last)/ 2;
            if ( a[mid] == x){
                return mid;
                }
                else{
                    if (a[mid] > x ) return binarySearch(a,x,first,mid);
                        else return binarySearch(a,x,mid+1,last);
                    }
            }
    }

int createVec(int *c, int *a, int n){
    int i = 0;
    int poz = -1;
    int length_b = 1;
    int *b;
    b = (int*) malloc(n*sizeof(int));
    b[0] = a[0];
    c[0] = 0;
    for (i = 1 ; i < n ; i++){
        poz = binarySearch(b,a[i],0,length_b -1);
        c[i] = poz;
        if ( poz == length_b )  {
            b[length_b] = a[i];
            length_b ++;}
        else{
            b[poz] = a[i];
            }
        }
    return length_b;
    }

int lastIndex(int *vec,int last,int look){
    int i;
    for (i = last ; i >= 0; i--){
        if( vec[i] == look){
            return i;
        }
    }
}



int main(int argc, char* argv[]){
    int* a;
    int* c;
    int* d;
    int i;
    int n,x,y;
    FILE *in;
    in = fopen("scmax.in","r");
    fscanf(in,"%d",&n);
    a = (int*) malloc(n*sizeof(int));
    c = (int*) malloc(n*sizeof(int));
    for (i = 0 ; i < n; i++){
        fscanf(in,"%d",&a[i]);
        }
    y = createVec(c,a,n);
    d = (int*) malloc(y*sizeof(int));
    int poz = n-1;
    for (i = (y-1); i >= 0; i--){
        poz = lastIndex(c,poz,i);
        d[i] = a[poz];
        }
    afisare(d,y);
    return 0;
}