結果

問題 No.3646 Decrement.
コンテスト
ユーザー Hari Aakash K
提出日時 2026-10-04 04:25:30
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 38 ms / 2,000 ms
+ 461µs
コード長 1,408 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,264 ms
コンパイル使用メモリ 366,560 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2026-10-04 04:25:49
合計ジャッジ時間 8,225 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
サンプル 0 % AC * 1
小課題1 5 % AC * 5
小課題2 5 % AC * 3
小課題3 20 % AC * 10
小課題4 30 % AC * 19
小課題5 10 % AC * 34
小課題6 30 % AC * 45
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// Full solution, O(N log N). Decompose the skyline into blocks (width w,
// h levels) with a monotonic stack; removing one level of a block costs w
// operations and saves 2. Take the cheapest levels first.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    int n; ll K;
    scanf("%d %lld", &n, &K);
    vector<ll> a(n + 2, 0);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);

    ll base = 0;                                   // cost / 2
    for (int i = 0; i <= n; i++) if (a[i + 1] > a[i]) base += a[i + 1] - a[i];

    vector<pair<ll, ll>> nodes;                    // (width, levels)
    vector<pair<ll, int>> st;                      // (height, left index)
    for (int i = 1; i <= n + 1; i++) {
        ll x = a[i];
        int left = i;
        while (!st.empty() && st.back().first > x) {
            auto [h, l] = st.back();
            st.pop_back();
            ll below = x;
            if (!st.empty()) below = max(below, st.back().first);
            nodes.push_back({(ll)(i - l), h - below});
            left = l;
        }
        if (x > 0 && (st.empty() || st.back().first < x)) st.push_back({x, left});
    }

    sort(nodes.begin(), nodes.end());
    ll gain = 0;
    for (auto& [w, h] : nodes) {
        ll t = min(h, K / w);
        gain += t;
        K -= t * w;
        if (t < h) break;
    }
    printf("%lld\n", 2 * (base - gain));
}
0