結果

問題 No.1435 Mmm......
ユーザー masutech16masutech16
提出日時 2021-03-22 23:42:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 4,100 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 210,332 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-03 12:55:58
合計ジャッジ時間 8,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 127 ms
6,940 KB
testcase_07 AC 151 ms
6,940 KB
testcase_08 AC 176 ms
6,944 KB
testcase_09 AC 167 ms
6,940 KB
testcase_10 AC 142 ms
6,940 KB
testcase_11 AC 142 ms
6,944 KB
testcase_12 AC 130 ms
6,944 KB
testcase_13 AC 129 ms
6,940 KB
testcase_14 AC 93 ms
6,940 KB
testcase_15 AC 183 ms
6,940 KB
testcase_16 AC 151 ms
6,940 KB
testcase_17 AC 105 ms
6,940 KB
testcase_18 AC 182 ms
6,940 KB
testcase_19 AC 135 ms
6,944 KB
testcase_20 AC 168 ms
6,944 KB
testcase_21 AC 115 ms
6,944 KB
testcase_22 AC 93 ms
6,944 KB
testcase_23 AC 294 ms
6,944 KB
testcase_24 AC 302 ms
6,944 KB
testcase_25 AC 289 ms
6,940 KB
testcase_26 AC 296 ms
6,944 KB
testcase_27 AC 314 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "/workspaces/compro/lib/data_structure/kth-smallest.hpp"
#include <cassert>
#include <iostream>
#include <vector>
#ifndef COMPRO_MATRIX
#define COMPRO_MATRIX

// 一点更新、一点削除、k番目に小さい値の取得ができるデータ構造を定義します

template <class T> class KthSmallest {
public:
  KthSmallest(int N) { data = std::vector<T>(N + 1, T(0)); }

  // 0-indexでi番目の要素にvalを加える
  void add(int index, T val) {
    // 内部的には1-indexで扱いたいので1足す
    index++;
    int N = data.size();
    for (int i = index; i <= N; i += i & -i) {
      data[i] += val;
    }
  }

  // 1-indexで昇順に並べたときのk番目の要素を返す
  T kth(int k) const {
    int N = data.size();
    assert(1 <= k && k <= N);
    int x = 0;
    int init = 1;
    while (2 * init <= N) {
      init *= 2;
    }
    for (int i = init; i > 0; i /= 2) {
      if (x + i <= N && data[x + i] < k) {
        k -= data[x + i];
        x += i;
      }
    }
    return x + 1;
  }

private:
  std::vector<T> data;
};

#endif
#line 1 "/workspaces/compro/lib/template.hpp"


#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 5 "/workspaces/compro/lib/utils/compress.hpp"
#ifndef COMPRO_COMPRESS
#define COMPRO_COMPRESS

template <class T> class Compress {
public:
  explicit Compress(const std::vector<T> &argv) {
    isBuilt = false;
    add(argv);
  }

  Compress() { isBuilt = false; }

  void add(const std::vector<T> &argv) {
    for (const auto &x : argv) {
      add(x);
    }
  }
  void add(const T x) {
    assert(!isBuilt);
    v.emplace_back(x);
  }

  void build() {
    isBuilt = true;
    std::sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
  }

  int get(const T x) const {
    assert(isBuilt);
    return lower_bound(v.begin(), v.end(), x) - v.begin();
  }

  const T &operator[](const int k) const { return v[k]; }

  int size() const { return v.size(); }

private:
  std::vector<T> v;
  bool isBuilt;
};
#endif
#line 4 "main.cpp"

long long solve(int N, const std::vector<int> &a) {
  Compress<int> cmp(a);
  cmp.build();

  KthSmallest<int> s(N);
  ll ans = 0;
  int right = 0;
  REP(left, N) {
    auto judge = [&]() {
      if (right - left < 2)
        return true;
      s.add(cmp.get(a[right]), 1);
      int m1 = cmp[s.kth(1) - 1];
      int m2 = cmp[s.kth(2) - 1];
      int M = cmp[s.kth(right + 1 - left) - 1];
      s.add(cmp.get(a[right]), -1);

      return M <= m1 + m2;
    };

    while (right < N && judge()) {
      s.add(cmp.get(a[right]), 1);
      right++;
    }
    ans += right - left - 1;
    s.add(cmp.get(a[left]), -1);
  }
  return ans;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  int N;
  std::cin >> N;
  std::vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> a[i];
  }
  auto ans = solve(N, a);
  std::cout << ans << std::endl;
  return 0;
}
0