Pagini recente » Cod sursa (job #1878417) | Cod sursa (job #2637386) | Cod sursa (job #1165432) | Cod sursa (job #524983) | Cod sursa (job #885537)
Cod sursa(job #885537)
#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 ReconstSol(int poz) {
if (poz == 0) return;
ReconstSol(Father[poz]);
fout << A[poz] << " ";
}
void WriteData() {
fout << LengthB << endl;
for (int i = 1; i <= N; ++i)
if (Best[i] == LengthB) {
ReconstSol(i);
return;
}
}
int main() {
ReadData();
Solve();
WriteData();
}