結果

問題 No.2548 Problem Selection
ユーザー 👑 emthrmemthrm
提出日時 2023-11-25 13:04:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,736 bytes
コンパイル時間 3,340 ms
コンパイル使用メモリ 265,772 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2023-11-25 13:04:57
合計ジャッジ時間 4,580 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
14,848 KB
testcase_01 AC 9 ms
6,784 KB
testcase_02 AC 32 ms
18,432 KB
testcase_03 AC 5 ms
6,676 KB
testcase_04 AC 20 ms
11,904 KB
testcase_05 AC 33 ms
18,304 KB
testcase_06 AC 5 ms
6,676 KB
testcase_07 AC 23 ms
14,464 KB
testcase_08 AC 26 ms
15,872 KB
testcase_09 AC 27 ms
16,640 KB
testcase_10 AC 33 ms
19,072 KB
testcase_11 AC 32 ms
19,072 KB
testcase_12 AC 32 ms
19,072 KB
testcase_13 WA -
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 WA -
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 WA -
testcase_25 AC 2 ms
6,676 KB
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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)
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 Semigroup, typename BinOp>
struct DisjointSparseTable {
  explicit DisjointSparseTable(const std::vector<Semigroup>& a,
                               const BinOp bin_op = BinOp())
      : bin_op(bin_op) {
    const int table_h = std::max(std::countr_zero(std::bit_ceil(a.size())), 1);
    lg.assign(1 << table_h, 0);
    for (int i = 2; i < (1 << table_h); ++i) {
      lg[i] = lg[i >> 1] + 1;
    }
    const int n = a.size();
    data.assign(table_h, std::vector<Semigroup>(n));
    std::copy(a.begin(), a.end(), data.front().begin());
    for (int i = 1; i < table_h; ++i) {
      const int shift = 1 << i;
      for (int left = 0; left < n; left += shift << 1) {
        const int mid = std::min(left + shift, n);
        data[i][mid - 1] = a[mid - 1];
        for (int j = mid - 2; j >= left; --j) {
          data[i][j] = bin_op(a[j], data[i][j + 1]);
        }
        if (n <= mid) break;
        const int right = std::min(mid + shift, n);
        data[i][mid] = a[mid];
        for (int j = mid + 1; j < right; ++j) {
          data[i][j] = bin_op(data[i][j - 1], a[j]);
        }
      }
    }
  }

  Semigroup query(const int left, int right) const {
    assert(left < right);
    if (left == --right) return data[0][left];
    const int h = lg[left ^ right];
    return bin_op(data[h][left], data[h][right]);
  }

 private:
  const BinOp bin_op;
  std::vector<int> lg;
  std::vector<std::vector<Semigroup>> data;
};

int main() {
  int n, m; cin >> n >> m;
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> a(n);
  for (int& a_i : a) cin >> a_i;
  ranges::sort(a);
  vector<ll> b(n - 1);
  REP(i, n - 1) b[i] = ll{a[i + 1] - a[i]} * (a[i + 1] - a[i]);
  const DisjointSparseTable<ll, plus<ll>> dst(b);
  ll ans = LINF;
  for (int i = 0; i + m - 1 < n - 1; ++i) {
    chmin(ans, dst.query(i, i + m - 1));
  }
  cout << ans << '\n';
  return 0;
}
0