Pagini recente » Profil mihnea_soitu | Cod sursa (job #2850976) | Cod sursa (job #2143569) | Cod sursa (job #2214729) | Cod sursa (job #1041443)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
const int N = 1e5 + 5;
int n, p[N], v[N], last[N], bst[N], hi = 1, AIB[N], tata[N], MAX;
int bs(int x) {
int lower = 1, higher = hi + 1;
while (lower <= higher) {
int mid = (lower + higher) >> 1;
if (last[mid] == x)
return mid;
else
if (last[mid] < x)
lower = mid + 1;
else
higher = mid;
}
return 0;
}
void Write(int x) {
if (tata[x])
Write(tata[x]);
fout << v[x] << " ";
}
void update (int x, int i) {
while (x <= n) {
if (bst[i] > bst[AIB[x]])
AIB[x] = i;
x += (x ^ (x - 1)) & x;
}
}
int query (int x) {
int MAX = 0;
while (x) {
if (bst[MAX] < bst[AIB[x]])
MAX = AIB[x];
x -= (x ^ (x - 1)) & x;
}
return MAX;
}
int main() {
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
last[i] = v[i];
}
fin.close();
sort (last+1, last + n + 1);
for (int i = 2; i <= n; ++i)
if (last[hi] != last[i])
last[++hi] = last[i];
for (int i = 1; i <= n; ++i)
p[i] = bs(v[i]);
for (int i = 1; i <= n; ++i) {
tata[i] = query (p[i] - 1);
bst[i] = bst[tata[i]] + 1;
update (p[i], i);
if (bst[i] > bst[MAX])
MAX = i;
}
fout << bst[MAX] << "\n";
Write(MAX);
fout.close();
}