結果
問題 |
No.3303 Heal Slimes 2
|
ユーザー |
|
提出日時 | 2025-10-07 15:09:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,798 ms / 4,000 ms |
コード長 | 1,695 bytes |
コンパイル時間 | 5,587 ms |
コンパイル使用メモリ | 257,704 KB |
実行使用メモリ | 7,716 KB |
最終ジャッジ日時 | 2025-10-07 15:09:52 |
合計ジャッジ時間 | 30,439 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template <class T> bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template <class T> bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; void solve() { int n, k, d; cin >> n >> k >> d; vector<int> h(n); rep(i, 0, n) cin >> h[i]; vector<int> ch = h; sort(ch.begin(), ch.end()); ch.erase(unique(ch.begin(), ch.end()), ch.end()); int m = ch.size(); auto get_ith = [&](int x) { return lower_bound(ch.begin(), ch.end(), x) - ch.begin(); }; atcoder::fenwick_tree<ll> ft(m), ct(m); auto add_st = [&](int x) { int i = get_ith(h[x]); ft.add(i, h[x]); ct.add(i, 1); }; auto del_st = [&](int x) { int i = get_ith(h[x]); ft.add(i, -h[x]); ct.add(i, -1); }; auto f = [&](int x) { int l = get_ith(x); int r = get_ith(x + d); ll res = 0; res += ct.sum(0, l) * x - ft.sum(0, l); res += ft.sum(r, m) - ct.sum(r, m) * (x + d); return res; }; rep(i, 0, k - 1) { add_st(i); } ll ans = 1e18; rep(i, k - 1, n) { add_st(i); ll up = 1e9 + 10; ll dw = -1; while (up - dw > 2) { ll md1 = (up + up + dw) / 3; ll md2 = (up + dw + dw) / 3; ll v1 = f(md1); ll v2 = f(md2); chmin(ans, v1); chmin(ans, v2); if (v1 < v2) dw = md2; else up = md1; } rep(x, dw + 1, up) chmin(ans, f(x)); del_st(i - k + 1); } cout << ans << '\n'; } int main() { int t = 1; // cin >> t; while (t--) solve(); }