結果

問題 No.738 平らな農地
ユーザー りあんりあん
提出日時 2018-09-28 21:54:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 2,340 bytes
コンパイル時間 2,550 ms
コンパイル使用メモリ 208,392 KB
実行使用メモリ 5,028 KB
最終ジャッジ日時 2023-08-02 11:18:42
合計ジャッジ時間 7,818 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 35 ms
4,380 KB
testcase_16 AC 36 ms
4,444 KB
testcase_17 AC 35 ms
4,672 KB
testcase_18 AC 35 ms
4,400 KB
testcase_19 AC 39 ms
4,516 KB
testcase_20 AC 36 ms
4,376 KB
testcase_21 AC 37 ms
4,380 KB
testcase_22 AC 36 ms
4,508 KB
testcase_23 AC 36 ms
4,504 KB
testcase_24 AC 36 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 40 ms
4,716 KB
testcase_46 AC 36 ms
4,512 KB
testcase_47 AC 38 ms
4,380 KB
testcase_48 AC 38 ms
4,776 KB
testcase_49 AC 37 ms
4,676 KB
testcase_50 AC 38 ms
4,628 KB
testcase_51 AC 38 ms
4,524 KB
testcase_52 AC 37 ms
4,500 KB
testcase_53 AC 38 ms
4,400 KB
testcase_54 AC 39 ms
4,452 KB
testcase_55 AC 39 ms
4,428 KB
testcase_56 AC 38 ms
4,492 KB
testcase_57 AC 36 ms
4,396 KB
testcase_58 AC 38 ms
4,380 KB
testcase_59 AC 40 ms
4,864 KB
testcase_60 AC 40 ms
4,800 KB
testcase_61 AC 39 ms
4,704 KB
testcase_62 AC 36 ms
4,668 KB
testcase_63 AC 40 ms
4,544 KB
testcase_64 AC 39 ms
4,376 KB
testcase_65 AC 33 ms
4,380 KB
testcase_66 AC 34 ms
4,376 KB
testcase_67 AC 45 ms
4,676 KB
testcase_68 AC 46 ms
4,884 KB
testcase_69 AC 48 ms
4,424 KB
testcase_70 AC 49 ms
4,796 KB
testcase_71 AC 27 ms
4,768 KB
testcase_72 AC 49 ms
4,504 KB
testcase_73 AC 47 ms
4,980 KB
testcase_74 AC 49 ms
4,596 KB
testcase_75 AC 48 ms
4,380 KB
testcase_76 AC 45 ms
4,512 KB
testcase_77 AC 50 ms
4,552 KB
testcase_78 AC 55 ms
4,432 KB
testcase_79 AC 51 ms
4,380 KB
testcase_80 AC 48 ms
4,512 KB
testcase_81 AC 44 ms
4,380 KB
testcase_82 AC 41 ms
4,376 KB
testcase_83 AC 44 ms
4,596 KB
testcase_84 AC 40 ms
4,612 KB
testcase_85 AC 42 ms
4,376 KB
testcase_86 AC 41 ms
4,376 KB
testcase_87 AC 43 ms
4,892 KB
testcase_88 AC 40 ms
5,028 KB
testcase_89 AC 2 ms
4,380 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    priority_queue<P> lower;
    priority_queue<P, vector<P>, greater<P>> upper;
    long long lower_sum = 0, upper_sum = 0;
    for (int i = 0; i < k; ++i) {
        upper.push(P(a[i], i));
        upper_sum += a[i];
    }
    while (lower.size() < upper.size()) {
        lower_sum += upper.top().first;
        upper_sum -= upper.top().first;
        lower.push(upper.top());
        upper.pop();
    }
    int lc = lower.size();
    int uc = upper.size();

    long long ans = 1e18;

    for (int i = k - 1; i < n; ++i) {
        if (i >= k) {
            if (P(a[i - k], i - k) > lower.top()) {
                --uc;
                upper_sum -= a[i - k];
            }
            else {
                --lc;
                lower_sum -= a[i - k];
            }
            P p = P(a[i], i);
            if (lower.top() > p) {
                ++lc;
                lower.push(p);
                lower_sum += a[i];
            }
            else {
                ++uc;
                upper.push(p);
                upper_sum += a[i];
            }
            while (lc > uc + 1) {
                if (lower.top().second <= i - k) {
                    lower.pop();
                    continue;
                }
                upper_sum += lower.top().first;
                lower_sum -= lower.top().first;
                ++uc;
                upper.push(lower.top());
                --lc;
                lower.pop();
            }
            while (lc < uc) {
                if (upper.top().second <= i - k) {
                    upper.pop();
                    continue;
                }
                lower_sum += upper.top().first;
                upper_sum -= upper.top().first;
                ++lc;
                lower.push(upper.top());
                --uc;
                upper.pop();
            }

        }
        while (lower.top().second <= i - k) {
            lower.pop();
        }
        long long m = lower.top().first;
        ans = min(ans, m * lc - lower_sum + upper_sum - m * uc);
        // cout << m * lc - lower_sum + upper_sum - m * uc << "\n";
    }
    cout << ans << "\n";
    return 0;
}
0