Pagini recente » Cod sursa (job #2590163) | Cod sursa (job #791132) | Cod sursa (job #1386755) | Cod sursa (job #1682278) | Cod sursa (job #1235851)
#include <fstream>
using namespace std;
const int MAXN = 100005;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int N;
int A[MAXN];
int Best[MAXN];
int Father[MAXN];
int B[MAXN], LengthB;
void ReadData()
{
fin >> N;
for (int i = 1; i <= N; ++i)
fin >> A[i];
}
void Solve()
{
B[ LengthB = 0 ] = 0;
int pow = 1;
for (int i = 1; i <= N; ++i)
{
while ((pow << 1) <= LengthB) pow <<= 1;
int ans = 0;
for (int stp = pow; stp; stp >>= 1)
if (ans + stp <= LengthB && A[ B[ans + stp] ] < A[i])
ans += stp;
Best[i] = Best[ B[ans] ] + 1;
Father[i] = B[ans];
if (ans == LengthB)
B[++LengthB] = i;
else
if (A[ B[ans + 1] ] > A[i])
B[ans + 1] = i;
}
}
void Rec(int i)
{
if (i == 0) return;
Rec(Father[i]);
fout << A[i] << " ";
}
void WriteData()
{
fout << LengthB << '\n';
for (int i = 1; i <= N; ++i)
{
if (Best[i] == LengthB)
{
Rec(i);
return;
}
}
}
int main()
{
ReadData();
Solve();
WriteData();
fin.close();
fout.close();
return 0;
}