結果

問題 No.738 平らな農地
ユーザー nebukuro09nebukuro09
提出日時 2018-09-28 23:51:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 2,512 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 159,628 KB
実行使用メモリ 14,996 KB
最終ジャッジ日時 2023-09-03 21:01:22
合計ジャッジ時間 11,205 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 109 ms
14,680 KB
testcase_16 AC 120 ms
13,724 KB
testcase_17 AC 113 ms
13,940 KB
testcase_18 AC 102 ms
12,900 KB
testcase_19 AC 88 ms
6,996 KB
testcase_20 AC 112 ms
13,092 KB
testcase_21 AC 106 ms
13,676 KB
testcase_22 AC 113 ms
13,128 KB
testcase_23 AC 109 ms
14,416 KB
testcase_24 AC 95 ms
7,172 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 121 ms
14,508 KB
testcase_46 AC 117 ms
13,784 KB
testcase_47 AC 117 ms
13,628 KB
testcase_48 AC 117 ms
13,700 KB
testcase_49 AC 116 ms
13,656 KB
testcase_50 AC 118 ms
13,968 KB
testcase_51 AC 118 ms
13,524 KB
testcase_52 AC 113 ms
13,008 KB
testcase_53 AC 115 ms
13,184 KB
testcase_54 AC 120 ms
13,484 KB
testcase_55 AC 120 ms
13,368 KB
testcase_56 AC 127 ms
13,944 KB
testcase_57 AC 112 ms
13,680 KB
testcase_58 AC 115 ms
13,412 KB
testcase_59 AC 131 ms
14,312 KB
testcase_60 AC 128 ms
14,996 KB
testcase_61 AC 126 ms
14,236 KB
testcase_62 AC 116 ms
13,280 KB
testcase_63 AC 124 ms
13,976 KB
testcase_64 AC 123 ms
13,524 KB
testcase_65 AC 16 ms
5,740 KB
testcase_66 AC 16 ms
5,936 KB
testcase_67 AC 107 ms
14,628 KB
testcase_68 AC 113 ms
14,480 KB
testcase_69 AC 84 ms
7,216 KB
testcase_70 AC 79 ms
14,456 KB
testcase_71 AC 48 ms
10,244 KB
testcase_72 AC 61 ms
12,452 KB
testcase_73 AC 61 ms
11,192 KB
testcase_74 AC 59 ms
10,372 KB
testcase_75 AC 90 ms
14,400 KB
testcase_76 AC 95 ms
14,368 KB
testcase_77 AC 84 ms
7,008 KB
testcase_78 AC 87 ms
7,228 KB
testcase_79 AC 68 ms
14,276 KB
testcase_80 AC 68 ms
14,288 KB
testcase_81 AC 57 ms
13,276 KB
testcase_82 AC 60 ms
7,280 KB
testcase_83 AC 63 ms
12,984 KB
testcase_84 AC 60 ms
11,196 KB
testcase_85 AC 49 ms
7,004 KB
testcase_86 AC 41 ms
6,888 KB
testcase_87 AC 87 ms
13,460 KB
testcase_88 AC 82 ms
12,808 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 1 ms
4,376 KB
testcase_91 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!long).array;

    if (N == 1 || K == 1) {
        writeln(0);
        return;
    }

    auto rbt_small = new RedBlackTree!(long, "a < b", true)();
    auto rbt_large = new RedBlackTree!(long, "a < b", true)();
    rbt_small.insert(-INF);
    rbt_large.insert(INF);

    long sum_small = 0;
    long sum_large = 0;
    long ans = INF;

    foreach (i; 0..N) {
        if (i >= K) {
            if (!rbt_small.equalRange(A[i-K]).empty) {
                rbt_small.removeKey(A[i-K]);
                sum_small -= A[i-K];
            } else {
                rbt_large.removeKey(A[i-K]);
                sum_large -= A[i-K];
            }
        }
        rbt_small.insert(A[i]);
        sum_small += A[i];
        while (rbt_small.back > rbt_large.front) {
            auto x = rbt_small.back;
            rbt_small.removeKey(x);
            rbt_large.insert(x);
            sum_small -= x;
            sum_large += x;
        }
        while (rbt_small.length >= rbt_large.length + 2) {
            auto x = rbt_small.back;
            rbt_small.removeKey(x);
            rbt_large.insert(x);
            sum_small -= x;
            sum_large += x;
        }
        while (rbt_large.length >= rbt_small.length + 2) {
            auto x = rbt_large.front;
            rbt_large.removeKey(x);
            rbt_small.insert(x);
            sum_small += x;
            sum_large -= x;
        }

        if (i < K - 1) {
            continue;
        }

        long a = rbt_small.length.to!long;
        long b = rbt_large.length.to!long;
        long mid1, mid2 = -1;

        if (K % 2 && rbt_small.length > rbt_large.length) {
            mid1 = rbt_small.back;
        } else if (K % 2 && rbt_large.length > rbt_small.length) {
            mid2 = rbt_large.front;
        } else {
            mid1 = (rbt_small.back + rbt_large.front) / 2;
            if ((rbt_small.back + rbt_large.front) % 2) {
                mid2 = mid1 + 1;
            }
        }

        ans = min(ans, mid1 * a - sum_small + sum_large - mid1 * b);
        if (mid2 >= 0) {
            ans = min(ans, mid2 * a - sum_small + sum_large - mid2 * b);
        }
    }

    ans.writeln;
}
0