Pagini recente » Cod sursa (job #1733757) | Istoria paginii utilizator/crisan_007 | Cod sursa (job #2831244) | Cod sursa (job #1906784) | Cod sursa (job #756492)
Cod sursa(job #756492)
#include <fstream>
#include <algorithm>
using namespace std;
long N;
long A[100005];
long L;
long ValBest[100005];
long IndexBest[100005];
long IndexParinte[100005];
long Result[100005];
long GetBestParinte(long i)
{
return (lower_bound(ValBest,ValBest + L + 1,A[i]) - ValBest);
}
int main(void)
{
fstream fin("scmax.in",ios::in);
fstream fout("scmax.out",ios::out);
long i,k;
fin >> N;
for (i = 1;i <= N;i += 1)
{
fin >> A[i];
ValBest[i] = 0X7FFFFFFF;
}
ValBest[i] = 0X7FFFFFFF;
L = 2;
ValBest[1] = A[1];
IndexBest[1] = 1;
IndexParinte[1] = 0;
for (i = 2;i <= N;i += 1)
{
k = GetBestParinte(i);
if (k == L)
{
ValBest[L] = A[i];
IndexBest[L] = i;
IndexParinte[i] = IndexBest[L - 1];
L += 1;
}
else
{
if (A[i] < ValBest[k])
{
ValBest[k] = A[i];
IndexParinte[i] = IndexBest[k - 1];
IndexBest[k] = i;
}
}
}
long Best = IndexBest[L - 1];
i = L - 1;
while (IndexParinte[Best] != 0)
{
Result[i] = A[Best];
Best = IndexParinte[Best];
i -= 1;
}
Result[i] = A[Best];
fout << (L - 1) << "\n";
for (i = 1;i < L;i += 1)
{
fout << Result[i] << " ";
}
fin.close();
fout.close();
return 0;
}