Pagini recente » Cod sursa (job #1453941) | Cod sursa (job #3188580) | Cod sursa (job #2577537) | Cod sursa (job #2717348) | Cod sursa (job #1244717)
#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;
}
else {
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];
}
}