結果

問題 No.738 平らな農地
ユーザー finefine
提出日時 2018-09-28 23:25:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 1,951 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 179,160 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-04-20 12:03:25
合計ジャッジ時間 9,758 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 112 ms
8,832 KB
testcase_16 AC 112 ms
8,960 KB
testcase_17 AC 125 ms
9,088 KB
testcase_18 AC 122 ms
8,704 KB
testcase_19 AC 147 ms
9,216 KB
testcase_20 AC 114 ms
8,832 KB
testcase_21 AC 132 ms
9,216 KB
testcase_22 AC 115 ms
8,960 KB
testcase_23 AC 131 ms
8,960 KB
testcase_24 AC 133 ms
8,960 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 122 ms
9,856 KB
testcase_46 AC 113 ms
9,344 KB
testcase_47 AC 116 ms
9,856 KB
testcase_48 AC 106 ms
9,472 KB
testcase_49 AC 105 ms
9,600 KB
testcase_50 AC 110 ms
9,728 KB
testcase_51 AC 127 ms
9,728 KB
testcase_52 AC 111 ms
9,600 KB
testcase_53 AC 115 ms
9,600 KB
testcase_54 AC 118 ms
9,728 KB
testcase_55 AC 119 ms
9,600 KB
testcase_56 AC 116 ms
9,728 KB
testcase_57 AC 107 ms
9,216 KB
testcase_58 AC 112 ms
9,600 KB
testcase_59 AC 119 ms
9,728 KB
testcase_60 AC 115 ms
9,856 KB
testcase_61 AC 115 ms
9,728 KB
testcase_62 AC 112 ms
9,216 KB
testcase_63 AC 126 ms
10,112 KB
testcase_64 AC 116 ms
9,728 KB
testcase_65 AC 128 ms
8,960 KB
testcase_66 AC 134 ms
9,216 KB
testcase_67 AC 95 ms
11,392 KB
testcase_68 AC 89 ms
11,648 KB
testcase_69 AC 111 ms
11,904 KB
testcase_70 AC 102 ms
12,160 KB
testcase_71 AC 11 ms
5,376 KB
testcase_72 AC 16 ms
5,376 KB
testcase_73 AC 15 ms
5,376 KB
testcase_74 AC 16 ms
5,376 KB
testcase_75 AC 110 ms
11,392 KB
testcase_76 AC 105 ms
11,648 KB
testcase_77 AC 117 ms
11,392 KB
testcase_78 AC 133 ms
12,032 KB
testcase_79 AC 122 ms
12,160 KB
testcase_80 AC 107 ms
11,904 KB
testcase_81 AC 112 ms
11,392 KB
testcase_82 AC 125 ms
11,904 KB
testcase_83 AC 16 ms
5,376 KB
testcase_84 AC 18 ms
5,376 KB
testcase_85 AC 174 ms
11,904 KB
testcase_86 AC 162 ms
11,392 KB
testcase_87 AC 96 ms
11,776 KB
testcase_88 AC 92 ms
11,392 KB
testcase_89 AC 2 ms
5,376 KB
testcase_90 AC 2 ms
5,376 KB
testcase_91 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

//引数unzipには圧縮前のvectorを入れる
int compress(vector<ll>& unzip, map<ll, int>& zip) {
    sort(unzip.begin(), unzip.end());
    unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
    for (int i = 0; i < unzip.size(); i++) {
        zip[unzip[i]] = i;
    }
    return unzip.size();
}

//1-indexであることに注意
template <typename T>
struct BIT {
    int n;
    vector<T> data;

    BIT(int n) : n(n), data(n + 1, 0) {}

    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

    void add(int i, T x) {
        while (i <= n) {
            data[i] += x;
            i += i & -i;
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector<ll> unzip(a);
    map<ll, int> zip;
    compress(unzip, zip);
    BIT<int> b(unzip.size());
    BIT<ll> b2(unzip.size());

    ll ans = 1000000000000000LL;
    for (int i = 0; i < n; i++) {
        b.add(zip[a[i]] + 1, 1);
        b2.add(zip[a[i]] + 1, a[i]);
        if (i < k - 1) continue;
        if (i >= k) {
            b.add(zip[a[i - k]] + 1, -1);
            b2.add(zip[a[i - k]] + 1, -a[i - k]);
        }
        int ok = (int)unzip.size() - 1, ng = -1;
        while (ok - ng > 1) {
            int mid = (ok + ng) / 2;
            if (b.sum(mid + 1) >= (k + 1) / 2) ok = mid;
            else ng = mid;
        }
        ll hoge = b2.sum(unzip.size()) - b2.sum(ok + 1);
        ll foo = b.sum(unzip.size()) - b.sum(ok + 1);
        ll tmp = hoge - foo * unzip[ok];
        hoge = b2.sum(ok);
        foo = b.sum(ok);
        tmp += foo * unzip[ok] - hoge;
        ans = min(ans, tmp);
        //cout << unzip[ok] << endl;
    }
    cout << ans << endl;
    return 0;
}
0