Cod sursa(job #1244702)

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

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

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

int main()
{
	int n, i;
	int *a;
	int result[3]={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[D]);
	for (i = result[S]; i <= result[F]; i++) {
		
		if (i != result[S] && a[i] == a[i-1])
			continue;
		fprintf(f, "%d ", a[i]);
	}	

	fclose(f);
	return 0;
}

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

	temp[D] = 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[D]++;
                        }
			temp[F]++;
		}
		else if (result[D] < temp[D]) {
			result[D] = temp[D];
			result[S] = temp[S];
			result[F] = temp[F];
			temp[D] = 1;
			temp[S] = temp[F] = i;
		}
	}

	if (result[D] < temp[D]) {
		result[D] = temp[D];
                result[S] = temp[S];
                result[F] = temp[F];
	}
}