Pagini recente » Cod sursa (job #2750042) | Cod sursa (job #2540005) | Cod sursa (job #1239480) | Cod sursa (job #812854) | Cod sursa (job #2614420)
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
#define in fin
#define out fout
using namespace std;
typedef long long ll;
typedef unsigned int uint;
const int nmax = 300005;
const ll linf = LLONG_MAX;
const ll mod = 1e9+7;
const int inf = INT_MAX;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n;
int a[nmax], b[nmax], parent[nmax];
unordered_map < int , int > compres;
pii bitdp[nmax];
pii query(int pos){
pii mx = {0,-1};
for(int i = pos ; i > 0 ; i -= i&(-i)){
if(mx.fr < bitdp[i].fr){
mx = bitdp[i];
}
}
return mx;
}
void update(int pos, int val , int index){
for(int i = pos ; i <= n ; i += i&(-i)){
if(bitdp[i].fr < val){
bitdp[i].sc = index;
bitdp[i].fr = val;
}
}
}
int main(){
in >> n;
for(int i = 1; i <= n; i++){
in >> a[i];
b[i] = a[i];
}
sort(b+1,b+n+1);
for(int i = 1 ; i <= n ; i++){
if(b[i] != b[i-1]){
compres[b[i]] = compres[b[i-1]] + 1;
}
}
for(int i = 1; i <= n; i++){
b[i] = compres[a[i]];
}
for(int i = 1 ; i <= n; i++){
pii mx = query(b[i]-1);
parent[i] = mx.sc;
update(b[i],mx.fr+1,i);
}
pii mx = query(n);
int start = mx.sc;
out << mx.fr << '\n';
vec < int > rez;
while(start != -1){
rez.pb(a[start]);
start = parent[start];
}
for(int i = rez.size() - 1; i >= 0 ; i--){
out << rez[i] << ' ';
}
// dp[i] = max(dp[j] | j < i && a[j] < a[i])
}