Pagini recente » Cod sursa (job #2600589) | Cod sursa (job #1055596) | Cod sursa (job #1287772) | Cod sursa (job #941132) | Cod sursa (job #1101030)
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int MAXN = 100005;
int lung[MAXN];
int v[MAXN], N, sol;
void Read() {
fin >> N;
for (int i = 1; i <= N; ++i)
fin >> v[i];
}
void BinSearch(int nr) {
int Left = 1; int Right = sol; int Middle;
while (Right - Left > 1) {
Middle = (Right - Left) / 2 + Left;
if (lung[Middle] > nr)
Left = Middle;
else
Right = Middle;
}
if (lung[Right] > nr) {
if (lung[Right + 1] < nr) {
lung[Right + 1] = nr;
++sol;
}
}else if (lung[Left] > nr) {
if (lung[Right] < nr)
lung[Right] = nr;
}
if (lung[1] < nr)
lung[1] = nr;
}
void Solve() {
lung[1] = v[N];
sol = 1;
for (int i = N - 1; i; --i)
BinSearch(v[i]);
}
void Write() {
fout << sol << '\n';
for (int i = sol; i; --i)
fout << lung[i] << ' ';
}
int main() {
Read();
Solve();
Write();
fin.close();
fout.close();
return 0;
}