結果

問題 No.2304 Distinct Elements
ユーザー ei1333333ei1333333
提出日時 2023-05-12 21:39:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 3,000 ms
コード長 4,872 bytes
コンパイル時間 2,410 ms
コンパイル使用メモリ 212,240 KB
実行使用メモリ 6,160 KB
最終ジャッジ日時 2023-08-19 04:11:46
合計ジャッジ時間 6,989 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 56 ms
5,948 KB
testcase_05 AC 55 ms
6,160 KB
testcase_06 AC 56 ms
6,016 KB
testcase_07 AC 56 ms
5,908 KB
testcase_08 AC 55 ms
5,824 KB
testcase_09 AC 107 ms
5,880 KB
testcase_10 AC 104 ms
5,828 KB
testcase_11 AC 106 ms
6,024 KB
testcase_12 AC 104 ms
5,900 KB
testcase_13 AC 107 ms
5,824 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 65 ms
5,700 KB
testcase_30 AC 6 ms
4,384 KB
testcase_31 AC 53 ms
4,700 KB
testcase_32 AC 81 ms
5,728 KB
testcase_33 AC 8 ms
4,384 KB
testcase_34 AC 83 ms
5,888 KB
testcase_35 AC 93 ms
5,872 KB
testcase_36 AC 6 ms
4,380 KB
testcase_37 AC 19 ms
4,384 KB
testcase_38 AC 61 ms
5,936 KB
testcase_39 AC 61 ms
5,872 KB
testcase_40 AC 33 ms
5,900 KB
testcase_41 AC 18 ms
4,384 KB
testcase_42 AC 45 ms
5,836 KB
testcase_43 AC 90 ms
6,004 KB
testcase_44 AC 75 ms
5,740 KB
testcase_45 AC 53 ms
4,520 KB
testcase_46 AC 41 ms
4,424 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,384 KB
testcase_49 AC 70 ms
5,828 KB
testcase_50 AC 77 ms
6,048 KB
testcase_51 AC 78 ms
5,832 KB
testcase_52 AC 60 ms
5,964 KB
testcase_53 AC 62 ms
5,832 KB
testcase_54 AC 52 ms
6,044 KB
testcase_55 AC 49 ms
6,044 KB
testcase_56 AC 47 ms
5,836 KB
testcase_57 AC 73 ms
5,832 KB
testcase_58 AC 72 ms
5,832 KB
testcase_59 AC 52 ms
5,824 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using int64 = long long;

// const int mod = 1e9 + 7;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;

const int inf = (1 << 30) - 1;

/*
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;
*/

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for (int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for (T &in: v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for (auto &e: t) fill_v(e, v);
}

template< typename F >
struct FixPoint: F {
  FixPoint(F &&f): F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

/**
 * @brief Slope-Trick
 * @see https://maspypy.com/slope-trick-1-%E8%A7%A3%E8%AA%AC%E7%B7%A8
 */
template< typename T >
struct SlopeQueue {

  const T INF = numeric_limits< T >::max() / 3;

  T min_f;

  priority_queue< T, vector< T >, less<> > L;

  priority_queue< T, vector< T >, greater<> > R;

  T add_l, add_r;

private:
  void push_R(const T &a) {
    R.push(a - add_r);
  }

  T top_R() const {
    return R.top() + add_r;
  }

  T pop_R() {
    T val = top_R();
    R.pop();
    return val;
  }

  void push_L(const T &a) {
    L.push(a - add_l);
  }

  T top_L() const {
    return L.top() + add_l;
  }

  T pop_L() {
    T val = top_L();
    L.pop();
    return val;
  }

  size_t size() {
    return L.size() + R.size() - 2;
  }

public:
  SlopeQueue(): min_f(0), add_l(0), add_r(0) {
    L.push(-INF);
    R.push(INF);
  }

  struct Query {
    T lx, rx, min_f;
  };

  // return min f(x)
  Query query() const {
    return (Query) {top_L(), top_R(), min_f};
  }

  // f(x) += a
  void add_all(const T &a) {
    min_f += a;
  }

  // add \_
  // f(x) += max(a - x, 0)
  void add_a_minus_x(const T &a) {
    min_f += max(T(0), a - top_R());
    push_R(a);
    push_L(pop_R());
  }

  // add _/
  // f(x) += max(x - a, 0)
  void add_x_minus_a(const T &a) {
    min_f += max(T(0), top_L() - a);
    push_L(a);
    push_R(pop_L());
  }

  // add \/
  // f(x) += abs(x - a)
  void add_abs(const T &a) {
    add_a_minus_x(a);
    add_x_minus_a(a);
  }

  // \/ -> \_
  // f_{new} (x) = min f(y) (y <= x)
  void clear_right() {
    while (R.size() >= 2) R.pop();
  }

  // \/ -> _/
  // f_{new} (x) = min f(y) (y >= x)
  void clear_left() {
    while (L.size() >= 2) L.pop();
  }

  // \/ -> \_/
  // f_{new} (x) = min f(y) (x-b <= y <= x-a)
  void shift(const T &a, const T &b) {
    assert(a <= b);
    add_l += a;
    add_r += b;
  }

  // \/. -> .\/
  // f_{new} (x) = f(x - a)
  void shift(const T &a) {
    shift(a, a);
  }

  // L, R を破壊する
  T get(const T &x) {
    T ret = min_f;
    while (L.size() >= 2) {
      ret += max(T(0), pop_L() - x);
    }
    while (R.size() >= 2) {
      ret += max(T(0), x - pop_R());
    }
    return ret;
  }

  void merge(SlopeQueue &st) {
    if (st.size() > size()) {
      swap(st.L, L);
      swap(st.R, R);
      swap(st.add_l, add_l);
      swap(st.add_r, add_r);
      swap(st.min_f, min_f);
    }
    while (st.R.size() >= 2) {
      add_x_minus_a(st.pop_R());
    }
    while (st.L.size() >= 2) {
      add_a_minus_x(st.pop_L());
    }
    min_f += st.min_f;
  }
};

int main() {
  int N;
  cin >> N;
  vector< int > C(N);
  cin >> C;
  sort(C.rbegin(), C.rend());
  for (int i = 0; i < N; i++) C[i] += i;
  SlopeQueue< int64 > dp;
  for (int i = N - 1; i >= 0; i--) {
    dp.clear_right();
    dp.add_abs(C[i]);
  }
  cout << dp.query().min_f << "\n";
}

0