Cod sursa(job #2575713)

Utilizator 700_or_disbandBest team 700_or_disband Data 6 martie 2020 15:07:52
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(int _:x)cerr<<_<<' ';cerr<<'\n',aaa
#define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
#define maxn 100000

using namespace std;

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

int v[maxn+5], pp[maxn+5], last[maxn+5];

int main () {
  int n; fin >> n;
  int i, j, z;
  for (i = 1; i <= n; i++) fin >> v[i];
  v[n+1] = INT_MAX;
  fill(all(pp), n+1); pp[0] = 0;
  int pas;
  for (i = 1; i <= n; i++) {
    for (j = 0, pas = (1<<17); pas > 0; pas /= 2)
      if (j+pas <= n && v[pp[j+pas]] < v[i]) j += pas;
    if (v[pp[j+1]] > v[i]) {
      pp[j+1] = i;
      last[i] = pp[j];
    }
  }
  int ans = 0;
  while (pp[ans+1] != n+1) ans++;
  fout << ans << '\n';
  vi sol; i = pp[ans];
  while (i != 0) {
    sol.pb(v[i]);
    i = last[i];
  }
  reverse(all(sol));
  for (int x: sol) fout << x << ' ';
  fin.close(); fout.close();
  return 0;
}