Pagini recente » Cod sursa (job #1181272) | Cod sursa (job #2910131)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define maxN 100002
int best[maxN], V[maxN], max, sol = 0, poz[maxN], P, N;
int print_best()
{
int i;
printf("\nbest: ");
for (i = 1; i <= N; i++)
printf("%d ", best[i]);
return 0;
}
int print_poz()
{
int i;
printf("\npoz: ");
for (i = 1; i <= N; i++)
printf("%d ", poz[i]);
return 0;
}
int main()
{
FILE *scmax_in, *scmax_out;
int i, j;
scmax_in = fopen("scmax.in", "r");
scmax_out = fopen("scmax.out", "w");
// Input
fscanf(scmax_in, "%d\n", &N);
for (i = 1; i <= N; i++)
fscanf(scmax_in, "%d ", &V[i]);
best[N] = 1;
poz[N] = -1;
max = 1;
P = N;
for (i = N - 1; i >= 1; --i) // Parcurgem vectorul invers
{
best[i] = 1;
poz[i] = -1;
for (j = i + 1; j <= N; ++j) // Pentru fiecare element din vector dupa pozitia curenta
if (V[i] < V[j] && best[i] < best[j] + 1) // Daca valoarea pozitiei curente < valoarea pozitiei precedente si lungimea curenta < lungimea precedenta + 1
{
best[i] = best[j] + 1;
poz[i] = j;
if (best[i] > max) {
max = best[i];
P = i;
}
//print_poz();
//print_best();
}
}
//output
fprintf(scmax_out, "%d\n", max); // Lmax
i = P;
while (i != -1)
{
fprintf(scmax_out, "%d ", V[i]);
i = poz[i];
}
fclose(scmax_in);
fclose(scmax_out);
return 0;
}