Cod sursa(job #1244697)

Utilizator luluzzaLucia Rabinca luluzza Data 17 octombrie 2014 23:46:00
Problema Subsir crescator maximal Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 1.22 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);

	longest(a, result, n);	

	f = fopen("scmax.out", "w");
	fprintf(f, "%d\n", result[U]);
	for (i = result[S]; i <= result[F]; i++) {
		
		if (i != result[S] && 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]) {
                        if ( a[temp[F]] != a[i] ) {
                                temp[U]++;
                        }
			temp[D]++;
			temp[F]++;
		}
		else if (result[D] < temp[D]) {
			result[D] = temp[D];
			result[S] = temp[S];
			result[F] = temp[F];
			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];
	}
}