Submission #1348892

#TimeUsernameProblemLanguageResultExecution timeMemory
1348892ramzialoulouFinancial Report (JOI21_financial)C++20
12 / 100
63 ms8128 KiB
#include <bits/stdc++.h>
 
using namespace std;

const int N = int(3e5) + 9;
const int LOG = 20;
int a[N];
int dp[N];
int seg[4 * N];

void upd(int nd, int l, int r, int p, int v) {
  if (l == r) {
    seg[nd] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid) {
    upd(nd * 2, l, mid, p, v);
  } else {
    upd(nd * 2 + 1, mid + 1, r, p, v);
  }
  seg[nd] = max(seg[nd * 2], seg[nd * 2 + 1]);
}

int get(int nd, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return seg[nd];
  }
  if (l > y || r < x) {
    return 0;
  }
  int mid = (l + r) >> 1;
  return max(get(nd * 2, l, mid, x, y), get(nd * 2 + 1, mid + 1, r, x, y));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, d;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> L(n);
  vector<int> stk;
  for (int i = 0; i < n; i++) {
    while (!stk.empty() && a[stk.back()] < a[i]) {
      stk.pop_back();
    }
    L[i] = (stk.empty() ? -1 : stk.back());
    while (!stk.empty() && a[stk.back()] == a[i]) {
      stk.pop_back();
    }
    stk.push_back(i);
  }
  int ans = 0;
  upd(1, 0, n - 1, 0, 1);
  for (int i = 1; i < n; i++) {
    int dp = 1;
    if (L[i] + 1 < i) {
      dp = get(1, 0, n - 1, L[i] + 1, i - 1) + 1;
    }
    upd(1, 0, n - 1, i, dp);
    ans = max(ans, dp);
  }
  cout << ans << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...