Pagini recente » Cod sursa (job #2740498) | Cod sursa (job #2790724) | Cod sursa (job #1948640) | Cod sursa (job #1522767) | Cod sursa (job #2727638)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5;
int n, a[NMAX + 1], dp[NMAX + 1], t[NMAX + 1], dpl;
inline int cb(const int QUERY) {
int ST = 1, DR = dpl, MIJ, ANS = DR + 1;
while(ST <= DR) {
MIJ = (ST + DR) / 2;
if(a[dp[MIJ]] >= a[QUERY]) DR = MIJ - 1, ANS = MIJ;
else ST = MIJ + 1;
}
return ANS;
}
int main()
{
freopen("scmax.in", "r", stdin);
freopen("scmax.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
dp[dpl = 1] = a[1];
for(int i = 2; i <= n; ++i) {
if(a[i] > a[dp[dpl]]) t[i] = dp[dpl], dp[++dpl] = i;
else {
const int poz = cb(i);
t[i] = dp[poz - 1], dp[poz] = i;
}
}
stack<int> st;
for(int i = dp[dpl]; t[i]; i = t[i]) st.push(i);
printf("%d\n", (int)st.size());
while(!st.empty()) printf("%d ", a[st.top()]), st.pop();
return 0;
}