Pagini recente » Cod sursa (job #2323759) | Cod sursa (job #2181212) | Cod sursa (job #2604289) | Cod sursa (job #932086) | Cod sursa (job #3038145)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5+2;
int n,o[NMAX],v[NMAX],dp[NMAX];
int aib[NMAX],t[NMAX];
map<int, int> mp;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int query(int pos){
int ans = 0;
for(int i = pos; i >= 1; i -= (i&-i)){
if(dp[aib[i]] > dp[ans]){
ans = aib[i];
}
}
return ans;
}
void update(int pos, int val){
for(int i = pos; i < NMAX; i += (i&-i)){
if(dp[aib[i]] < dp[val]){
aib[i] = val;
}
}
}
int main()
{
fin >> n;
for(int i = 1; i <= n; i++){
fin >> o[i];
mp[o[i]] = i;
}
int cnt = 0;
for(auto &it: mp){
it.second = ++cnt;
}
for(int i = 1; i <= n; i++){
v[i] = mp[o[i]];
}
int pos = 0, maxi = 0;
for(int i = 1; i <= n; i++){
t[i] = query(v[i]-1);
dp[i] = 1+dp[t[i]];
update(v[i], i);
if(maxi < dp[i]){
maxi = dp[i];
pos = i;
}
}
fout << maxi << "\n";
vector<int> sol;
while(pos){
sol.push_back(v[pos]);
pos = t[pos];
}
reverse(sol.begin(), sol.end());
vector<int> norm;
for(auto [key, val]: mp){
norm.push_back(key);
}
for(int it: sol){
fout << norm[it-1] << " ";
}
return 0;
}