結果
| 問題 |
No.2548 Problem Selection
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-11-25 13:04:52 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,736 bytes |
| コンパイル時間 | 3,343 ms |
| コンパイル使用メモリ | 264,288 KB |
| 実行使用メモリ | 18,944 KB |
| 最終ジャッジ日時 | 2024-09-26 10:21:36 |
| 合計ジャッジ時間 | 4,881 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 21 WA * 4 |
ソースコード
#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;
}
emthrm