結果

問題 No.1188 レベルX門松列
ユーザー iqueue02iqueue02
提出日時 2020-08-22 16:59:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 2,255 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 210,708 KB
実行使用メモリ 5,680 KB
最終ジャッジ日時 2023-08-05 13:54:04
合計ジャッジ時間 4,721 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
4,692 KB
testcase_01 AC 130 ms
4,884 KB
testcase_02 AC 113 ms
4,688 KB
testcase_03 AC 156 ms
5,168 KB
testcase_04 AC 121 ms
5,680 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 16 ms
4,760 KB
testcase_07 AC 113 ms
5,112 KB
testcase_08 AC 112 ms
5,404 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 13 ms
4,376 KB
testcase_12 AC 12 ms
4,380 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 99 ms
4,452 KB
testcase_15 AC 176 ms
5,632 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 135 ms
5,248 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 125 ms
5,268 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;

  T f(T a, T b) {return max(a, b);}

  SegmentTree (int sz, T init = 0) {
    n = 1; while (n < sz) n <<= 1;
    node.assign(2*n, init);
    for (int i = n - 2; i >= 0; i--) node[i] = f(node[2*i+1], node[2*i+2]);
  }

  void update(int k, T x) {
    k += (n - 1);
    node[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }

  void clear(T init = 0) {
    for (int i = 0; i < 2 * n; i++) node[i] = init;
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }

  T getvalue(int k) {
    return query(k,k+1);
  }

};
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  auto b = a;
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());

  int m = b.size();
  int res = 0;
  SegmentTree<int> seg(m);

  vector<int> dpl(n, 0), dpr(n, 0);

  auto make_id = [&](int x){ 
    return lower_bound(b.begin(), b.end(), x) - b.begin();
  };

  for (int i = 0; i < n; i++) {
    int id = make_id(a[i]);
    dpl[i] = seg.query(id + 1, m) + 1;
    seg.update(id, dpl[i]);
  }

  seg.clear();

  for (int i = n - 1; i >= 0; i--) {
    int id = make_id(a[i]);
    dpr[i] = seg.query(id + 1, m) + 1;
    seg.update(id, dpr[i]);
  }

  for (int i = 0; i < n; i++) {
    res = max(res, min(dpl[i], dpr[i]));
  }
  
  for (int i = 0; i < n; i++) dpl[i] = 0, dpr[i] = 0;
  seg.clear();

  for (int i = n - 1; i >= 0; i--) {
    int id = make_id(a[i]);
    dpr[i] = seg.query(0, id) + 1;
    seg.update(id, dpr[i]);
  }

  seg.clear();

  for (int i = 0; i < n; i++) {
    int id = make_id(a[i]);
    dpl[i] = seg.query(0, id) + 1;
    seg.update(id, dpl[i]);
  }
  
  for (int i = 0; i < n; i++) {
    res = max(res, min(dpl[i], dpr[i]));
  }

  cout << res - 1 << endl;
  return 0;
}
0