結果

問題 No.838 Noelちゃんと星々3
ユーザー yakamotoyakamoto
提出日時 2019-06-14 22:53:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,321 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 169,924 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 18:11:30
合計ジャッジ時間 2,637 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
6,940 KB
testcase_15 WA -
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
6,944 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

namespace DECLARATIONS {
  using namespace std;

  using ll = long long;
  using PI = pair<int, int>;
  template<class T> using V = vector<T>;
  using VI = V<int>;
#define _1 first
#define _2 second

#ifdef MY_DEBUG
# define DEBUG(x) x
#else
# define DEBUG(x)
#endif

  template<class T>
  inline void debug(T &A) {
    DEBUG(
        for (const auto &a : A) {
          cerr << a << " ";
        }
        cerr << '\n';
    )
  }

  template<class T>
  inline void debug_dim2(T &A) {
    DEBUG(
        for (const auto &as : A) {
          debug(as);
        }
    )
  }

  template<typename ... Args>
  inline void debug(const char *format, Args const &... args) {
    DEBUG(
        fprintf(stderr, format, args ...);
        cerr << '\n';
    )
  }

  template<typename ... Args>
  string format(const std::string &fmt, Args ... args) {
    size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args ...);
    std::vector<char> buf(len + 1);
    std::snprintf(&buf[0], len + 1, fmt.c_str(), args ...);
    return std::string(&buf[0], &buf[0] + len);
  }
}
using namespace DECLARATIONS;

const int MOD = 1000000007;

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  VI A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());

  ll INF = (ll)1e18;
  function<ll(int, int, ll)> dfs = [&](int l, int r, ll sum) {
    debug("dfs(%d, %d, %lld)", l, r, sum);

    // 分割するには4以上必要
    if (r - l <= 3) return sum;

    ll sum1 = 0ll;
    ll sum2 = sum;
    int ix = -1;
    ll _sum1 = INF, _sum2 = INF;

    int n = r - l;
    for (int i = 0; i < n - 1; ++i) {
      int med1 = l + i/2;
      int med2 = l + (i + n) / 2;
      sum1 += A[l + i] - A[med1];
      sum2 -= A[med2] - A[l + i];
      debug("i:%d med1:%d med2:%d sum1:%lld sum2:%lld", i, med1, med2, sum1, sum2);
      if (min(i + 1, n - 1 - i) >= 2 && sum1 + sum2 < _sum1 + _sum2) {
        _sum1 = sum1;
        _sum2 = sum2;
        ix = i + 1;
      }
    }

    if (ix == -1) return sum; // 分割できなかった
    return dfs(l, l + ix, _sum1) + dfs(l + ix, r, _sum2);
  };

  int m = N / 2;
  ll sum = accumulate(A.begin(), A.end(), 0ll, [&](ll acc, int a){
    return acc + abs(a - A[m]);
  });

  cout << dfs(0, N, sum);

  return 0;
}
0