Pagini recente » Cod sursa (job #3237144) | Cod sursa (job #1312883) | Cod sursa (job #126872) | Cod sursa (job #1725554) | Cod sursa (job #2575984)
#include <fstream>
#include <stack>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
const int MAXN = 1e5;
const int MAXVAL = 1e9;
int n;
int v[MAXN + 1], pre[MAXN + 1], lung[MAXN + 1];
struct tip {
int poz, val;
tip(int _poz = 0, int _val = MAXVAL) {
poz = _poz;
val = _val;
}
}dp[MAXN + 1];
int cb(int val) {
int ret = 0;
int add = 1 << 20;
while (add) {
if (ret + add <= n && dp[ret + add].val < val) ret += add;
add >>= 1;
}
return ret;
}
int main() {
in >> n;
for (int i = 1; i <= n; ++ i) {
in >> v[i];
lung[i] = 1;
}
int ans = 1;
for (int i = 1; i <= n; ++ i) {
lung[i] = cb(v[i]) + 1;
pre[i] = dp[lung[i] - 1].poz;
ans = max(ans, lung[i]);
if (v[i] < dp[lung[i]].val) {
dp[lung[i]].val = v[i];
dp[lung[i]].poz = i;
}
}
stack<int> afis;
afis.push(dp[ans].poz);
while (pre[afis.top()] != 0) {
afis.push(pre[afis.top()]);
}
out << ans << '\n';
while (afis.size()) {
out << v[afis.top()] << ' ';
afis.pop();
}
return 0;
}