Pagini recente » Cod sursa (job #149439) | Cod sursa (job #404990) | Cod sursa (job #1685882) | Cod sursa (job #2827033) | Cod sursa (job #2415769)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int v[MAXN], last[MAXN];
vector<int> dp, ans;
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 pas = 1 << 30, rez = -1;
while(pas){
if(rez + pas < int(dp.size()) && v[dp[pas + rez]] < v[i])
rez += pas;
pas /= 2;
}
rez++;
last[i] = dp[rez - 1];
dp[rez] = i;
}
int current = dp.back();
while(current){
ans.push_back(v[current]);
current = last[current];
}
fout << ans.size() << "\n";
reverse(ans.begin(), ans.end());
for(int i = 0; i < int(ans.size()); ++i)
fout << ans[i] << " ";
return 0;
}