Pagini recente » Cod sursa (job #3174681) | Cod sursa (job #1445002) | Cod sursa (job #2521910) | Cod sursa (job #2906509) | Cod sursa (job #2385211)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int v[MAXN], last[MAXN];
vector<int> dp, ans;
int len;
int main()
{
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n;
fin >> n;
for(int i = 1; i <= n; ++i)
fin >> v[i];
dp.push_back(0);
for(int i = 1; i <= n; ++i){
if(v[i] > v[dp.back()]){
last[i] = dp.back();
dp.push_back(i);
continue;
}
int rez = 0;
for(int exp = 30; exp >= 0; --exp){
int pos = rez + (1 << exp);
if(pos < int(dp.size()) && v[dp[pos]] < v[i])
rez += (1 << exp);
}
last[i] = dp[rez - 1];
dp[rez] = i;
}
int curent = dp.back();
while(curent){
ans.push_back(v[curent]);
curent = last[curent];
}
fout << ans.size() << "\n";
reverse(ans.begin(), ans.end());
for(auto x : ans)
fout << x << " ";
return 0;
}