Pagini recente » Cod sursa (job #2511297) | Cod sursa (job #1540070) | Cod sursa (job #2074394) | Cod sursa (job #177906) | Cod sursa (job #2613672)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#define int long long
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const ll INF64 = 3e18 + 1;
const int N = 2e5 + 11;
int a[N],ind[N],pos[N],n,top;
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ifstream cin("scmax.in");
ofstream cout("scmax.out");
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
ind[1] = 1;
pos[1] = 1;
top = 1;
for(int i = 2; i <= n; i++){
int x = a[i];
if(x < a[ind[1]]){
ind[1] = i;
pos[i] = i;
}else
if(x > a[ind[top]]){
pos[i] = ind[top];
ind[++top] = i;
}else{
int l = 1; int r = top;
while(l <= r){
int mid = (l+r)/2;
if(a[ind[mid]] < x && a[ind[mid + 1]] > x){
ind[mid + 1] = i;
pos[i] = ind[mid];
l = r + 1;
}else
if(a[ind[mid]] < x)l = mid + 1;else r = mid - 1;
}
}
}
for(int i = 1; i <= n; i++){
//cout << ind[i] << ' ';
}
for(int i = 1; i <= n; i++){
//cout << pos[i] << ' ';
}
cout << top <<'\n';
int x = ind[top];
vector<int>v;
while(x != pos[x]){
v.pb(pos[x]);
x = pos[x];
}
sort(all(v));
for(auto it: v)cout << a[it] << ' '; cout << a[ind[top]] << ' ';
return 0;
}
// 11 3 4 -1 5 8 2 3 12 7 9 10