結果

問題 No.2304 Distinct Elements
ユーザー 👑 emthrmemthrm
提出日時 2023-05-13 01:56:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 3,000 ms
コード長 3,181 bytes
コンパイル時間 3,352 ms
コンパイル使用メモリ 261,224 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2023-08-19 07:33:33
合計ジャッジ時間 7,447 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 38 ms
5,892 KB
testcase_05 AC 38 ms
5,920 KB
testcase_06 AC 38 ms
5,868 KB
testcase_07 AC 39 ms
5,936 KB
testcase_08 AC 38 ms
5,916 KB
testcase_09 AC 44 ms
5,960 KB
testcase_10 AC 43 ms
6,016 KB
testcase_11 AC 43 ms
5,928 KB
testcase_12 AC 43 ms
5,880 KB
testcase_13 AC 43 ms
5,848 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 32 ms
5,672 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 26 ms
4,524 KB
testcase_32 AC 35 ms
5,752 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 36 ms
5,784 KB
testcase_35 AC 40 ms
5,792 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 13 ms
4,376 KB
testcase_38 AC 41 ms
5,992 KB
testcase_39 AC 42 ms
5,868 KB
testcase_40 AC 27 ms
5,672 KB
testcase_41 AC 14 ms
4,380 KB
testcase_42 AC 33 ms
5,868 KB
testcase_43 AC 43 ms
5,844 KB
testcase_44 AC 31 ms
5,692 KB
testcase_45 AC 24 ms
4,560 KB
testcase_46 AC 19 ms
4,400 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 39 ms
5,852 KB
testcase_50 AC 41 ms
5,872 KB
testcase_51 AC 42 ms
5,892 KB
testcase_52 AC 37 ms
5,896 KB
testcase_53 AC 27 ms
5,928 KB
testcase_54 AC 35 ms
5,868 KB
testcase_55 AC 34 ms
5,884 KB
testcase_56 AC 34 ms
5,916 KB
testcase_57 AC 37 ms
5,880 KB
testcase_58 AC 37 ms
5,848 KB
testcase_59 AC 34 ms
5,896 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct SlopeTrick {
  const T inf;

  explicit SlopeTrick(
      const T min_f = 0, const T inf = std::numeric_limits<T>::max())
      : inf(inf), added_l(0), added_r(0), min_f(min_f) {}

  T min() const { return min_f; }
  std::pair<T, T> argmin() const { return {top_l(), top_r()}; }

  template <typename U>
  U f(const U x) {
    U f_x = min_f;
    std::vector<T> tmp;
    for (; top_l() > x; l.pop()) {
      const T t = top_l();
      f_x += t - x;
      tmp.emplace_back(t);
    }
    for (; !tmp.empty(); tmp.pop_back()) {
      emplace_l(tmp.back());
    }
    for (; top_r() < x; r.pop()) {
      const T t = top_r();
      f_x += x - t;
      tmp.emplace_back(t);
    }
    for (; !tmp.empty(); tmp.pop_back()) {
      emplace_r(tmp.back());
    }
    return f_x;
  }

  void constant_function(const T c) { min_f += c; }

  void x_minus_a(const T a) {
    const T t = top_l();
    if (t <= a) {
      emplace_r(a);
    } else {
      min_f += t - a;
      emplace_l(a);
      l.pop();
      emplace_r(t);
    }
  }

  void a_minus_x(const T a) {
    const T t = top_r();
    if (a <= t) {
      emplace_l(a);
    } else {
      min_f += a - t;
      emplace_r(a);
      r.pop();
      emplace_l(t);
    }
  }

  void abs_x_minus_a(const T a) {
    x_minus_a(a);
    a_minus_x(a);
  }

  void cumulative_min() {
    while (!r.empty()) r.pop();
    added_r = 0;
  }

  void rcumulative_min() {
    while (!l.empty()) l.pop();
    added_l = 0;
  }

  void translate(const T a) { sliding_window_minimum(a, a); }

  void sliding_window_minimum(const T a, const T b) {
    assert(a <= b);
    added_l += a;
    added_r += b;
  }

 private:
  T added_l, added_r, min_f;
  std::priority_queue<T> l;
  std::priority_queue<T, std::vector<T>, std::greater<T>> r;

  void emplace_l(const T a) { l.emplace(a - added_l); }
  void emplace_r(const T a) { r.emplace(a - added_r); }

  T top_l() const { return l.empty() ? -inf : l.top() + added_l; }
  T top_r() const { return r.empty() ? inf : r.top() + added_r; }
};

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  ranges::sort(a);
  SlopeTrick<ll> slope_trick(0);
  REP(i, n) {
    slope_trick.cumulative_min();
    slope_trick.abs_x_minus_a(a[i] - i);
  }
  cout << slope_trick.min() << '\n';
  return 0;
}
0