Cod sursa(job #3238495)

Utilizator Traian_7109Traian Mihai Danciu Traian_7109 Data 25 iulie 2024 20:07:40
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <stdio.h>
#include <algorithm>

#define MAXN 100000

struct Element {
  int val, poz;
} v[MAXN + 1];
static inline int compar(Element a, Element b) {
  return a.val < b.val;
}
int vnou[MAXN + 1], dp[MAXN + 1], next[MAXN + 1], ans[MAXN + 1], vold[MAXN + 1];

struct FenwickTree {
  int aib[MAXN + 1], n;
  
  void init(int n) {
    int i;
    
    this->n = n;
    for(i = 0; i <= n; i++) {
      aib[i] = 0;
    }
  }
  
  void update(int poz, int from) {
    do {
      if(dp[from] > dp[aib[poz]]) {
        aib[poz] = from;
      }
      poz += poz & -poz;
    } while(poz <= n);
  }
  
  int query(int poz) {
    int ans = 0;
    while(poz > 0) {
      if(dp[aib[poz]] > dp[ans]) {
        ans = aib[poz];
      }
      poz &= poz - 1;
    }
    return ans;
  }
} aib;

int main() {
  int n, i, cnt, max;
  
  #ifndef LOCAL
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
  #endif
  
  scanf("%d", &n);
  for(i = 1; i <= n; i++) {
    scanf("%d", &v[i].val);
    v[i].poz = i;
    vold[i] = v[i].val;
  }
  
  std::sort(v + 1, v + n + 1, compar);
  cnt = 0;
  for(i = 1; i <= n; i++) {
    if(v[i].val > v[i - 1].val) {
      cnt++;
    }
    vnou[v[i].poz] = cnt;
  }
  
  max = 0;
  aib.init(cnt);
  for(i = 1; i <= n; i++) {
    next[i] = aib.query(vnou[i] - 1);
    dp[i] = dp[next[i]] + 1;
    aib.update(vnou[i], i);
    if(dp[i] > dp[max]) {
      max = i;
    }
  }
  
  printf("%d\n", dp[max]);
  cnt = 0;
  while(max > 0) {
    ans[cnt++] = vold[max];
    max = next[max];
  }
  for(i = cnt - 1; i >= 0; i--) {
    printf("%d ", ans[i]);
  }
  fputc('\n', stdout);
  return 0;
}