Cod sursa(job #1244691)

Utilizator luluzzaLucia Rabinca luluzza Data 17 octombrie 2014 23:37:38
Problema Subsir crescator maximal Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 1.45 kb
#include <stdio.h>
#include <stdlib.h>

#define D	0
#define S	1
#define F	2
#define U	3

void longest(int *, int *, int);

int main()
{
	int n, i;
	int *a;
	int result[4]={0};

	FILE *f = fopen("scmax.in", "r");
	fscanf(f, "%d", &n);

	a = (int *)calloc(n, sizeof(int));

	for (i = 0; i < n; i++) {
		fscanf(f, "%d", &(a[i]));
	}
	fclose(f);

        printf("%d\n", n);
        for (i = 0; i < n; i++) {
                printf("%d ", a[i]);
        }
printf("\n");
	longest(a, result, n);	

	f = fopen("scmax.out", "w");
	fprintf(f, "%d\n", result[U]);
	for (i = result[S]; i <= result[F]; i++) {
		if (a[i] != a[i-1])
			fprintf(f, "%d ", a[i]);
	}	

	fclose(f);
	return 0;
}

void longest( int *a, int *result, int n )
{
	int temp[4]={0};
	int i;

	temp[D] = temp[U] = 1;
	temp[S] = temp[F] = 0;

	for (i = 1; i < n; i++) {
		if (a[temp[F]] <= a[i]) {
			printf("i = %d |  %d | %d |||| ", i, a[temp[F]], a[i]);
			temp[D]++;
			temp[F]++;
			if ( a[i-1] != a[i] ) {
				printf("temp = %d \n", temp[U]);
				temp[U]++;
			}
			printf("\n");
		}
		else if (result[D] < temp[D]) {
			result[D] = temp[D];
			result[S] = temp[S];
			result[F] = temp[F];
			printf("step %d | changing %d | %d\n", i, result[U], temp[U]);
			result[U] = temp[U];
			temp[D] = temp[U] = 1;
			temp[S] = temp[F] = i;
		}
	}

	if (result[D] < temp[D]) {
		result[D] = temp[D];
                result[S] = temp[S];
                result[F] = temp[F];
		result[U] = temp[U];
	}
	printf("%d", temp[U]);
}