Pagini recente » Cod sursa (job #1458748) | Cod sursa (job #2666936) | Cod sursa (job #1369933) | Cod sursa (job #1513746) | Cod sursa (job #2696875)
#include <stdio.h>
#include <iostream>
using namespace std;
const int NMAX = 1e5, STEPVALUE = (1 << 16), INFINIT = 2e9 + 1;
int v[NMAX], smax[NMAX], dp[NMAX], sol[NMAX];
int main()
{
int n, i, step, pos, cntmax = -1, j, ultelem;
FILE *fin = fopen("scmax.in", "r");
fscanf(fin, "%d", &n);
for (i = 0; i < n; i++)
{
fscanf(fin, "%d", &v[i]);
step = STEPVALUE;
pos = -1;
while (step > 0)
{
if (pos + step <= cntmax && v[smax[pos + step]] < v[i])
pos += step;
step >>= 1;
}
if (pos == cntmax)
cntmax++;
smax[pos + 1] = i;
dp[i] = pos + 1;
}
fclose(fin);
FILE *fout = fopen("scmax.out", "w");
fprintf(fout, "%d\n", cntmax + 1);
i = n - 1;
j = -1;
ultelem = INFINIT;
while (cntmax >= 0)
{
if (dp[i] == cntmax && ultelem > v[i])
{
ultelem = v[i];
cntmax--;
sol[++j] = i;
}
i--;
}
for (j; j >= 0; j--)
fprintf(fout, "%d ", v[sol[j]]);
fclose(fout);
return 0;
}