Cod sursa(job #2877959)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 25 martie 2022 17:25:48
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
#include <bits/stdc++.h>
	
using namespace std;
	
 
	
typedef long long ll;
	
#define f first
	
#define s second
	
#define pb push_back
	
#define mp make_pair
	
#define pi pair<ll, ll>
	
#define sz(x) (int)((x).size())
	
#define all(a) (a).begin(), (a).end()
	
 
	
/*const ll maxn = 1e5;
	
int f[maxn],nf[maxn],inv[maxn];
	
const int M=998244353;
	
void init(){
	
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
	
    f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
	
}
	
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;} */
	
 
	
const ll mod = 1e9+7;
	
ll n, k, m, mi, ma;
	
 
	
void solve(){
	
	
	
	ifstream cin("scmax.out");
	
	ofstream cout("scmax.out");
	
	cin >> n;
	
	vector<ll> L(n + 500, -1), b(n + 500, -1), a(n + 500), ans(n + 500);
	
	for(int i = 0; i<n; i++){
	
		cin >> a[i];
	
	}
	
	ll w = 1;
	
	L[1] = 0;
	
	ans[0] = 1;
	
	for(int i = 1; i<n; i++){
	
		
	
		ll l = 1, r = w;
	
		ma = 0;
	
		while(l <= r){
	
			ll x = (l + r)/2;
	
			if(a[L[x]] >= a[i]){
	
				r = x - 1;
	
				continue;
	
			}
	
			l = x + 1;
	
			ma = max(ma, x);
	
		}
	
		ll x = ma;
	
		ans[i] = x + 1;
	
		b[i] = L[x];
	
		if(w == x) w++;
	
		if(L[x + 1] == -1){
	
			L[x + 1] = i;
	
			continue;
	
		} 
	
		if(a[L[x + 1]] > a[i]) L[x + 1] = i;
	
	}
	
	ma = 0;
	
	for(int i = 0; i<n; i++){
	
		if(ans[ma] > ans[i]) continue;
	
		ma = i;
	
	}
	
	vector<ll> c;
	
	while(ma != -1){
	
		c.pb(ma);
	
		ma = b[ma];
	
	}
	
	reverse(all(c));
	
	cout << c.size() << "\n";
	
	for(int i = 0; i<c.size(); i++) cout << a[c[i]] << " "; cout << "\n";
	
	//cout << w << "\n";
	
//	for(int i = 0; i<n; i++) cout << ans[i] << " "; cout << "\n";
	
}
	
 
	
int main(){
	
	ios::sync_with_stdio(0);
	
	cin.tie(0);
	
 
	
	//init();
	
	
	
	int t=1;
	
//	cin >> t;
	
	while(t--) solve();
	
	
	
	return 0;
	
}