結果
問題 | No.738 平らな農地 |
ユーザー | nebukuro09 |
提出日時 | 2018-09-28 23:51:25 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 110 ms / 2,000 ms |
コード長 | 2,512 bytes |
コンパイル時間 | 1,676 ms |
コンパイル使用メモリ | 159,744 KB |
実行使用メモリ | 14,088 KB |
最終ジャッジ日時 | 2024-06-13 01:41:49 |
合計ジャッジ時間 | 9,967 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 3 ms
6,944 KB |
testcase_06 | AC | 3 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,944 KB |
testcase_13 | AC | 3 ms
6,944 KB |
testcase_14 | AC | 2 ms
6,940 KB |
testcase_15 | AC | 91 ms
12,908 KB |
testcase_16 | AC | 110 ms
13,284 KB |
testcase_17 | AC | 107 ms
13,240 KB |
testcase_18 | AC | 95 ms
12,496 KB |
testcase_19 | AC | 83 ms
6,944 KB |
testcase_20 | AC | 102 ms
12,556 KB |
testcase_21 | AC | 96 ms
13,460 KB |
testcase_22 | AC | 104 ms
12,484 KB |
testcase_23 | AC | 104 ms
13,372 KB |
testcase_24 | AC | 83 ms
6,940 KB |
testcase_25 | AC | 1 ms
6,940 KB |
testcase_26 | AC | 1 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | AC | 2 ms
6,940 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 1 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,944 KB |
testcase_32 | AC | 2 ms
6,940 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 2 ms
6,940 KB |
testcase_35 | AC | 2 ms
6,940 KB |
testcase_36 | AC | 1 ms
6,940 KB |
testcase_37 | AC | 2 ms
6,944 KB |
testcase_38 | AC | 2 ms
6,944 KB |
testcase_39 | AC | 2 ms
6,940 KB |
testcase_40 | AC | 2 ms
6,940 KB |
testcase_41 | AC | 2 ms
6,940 KB |
testcase_42 | AC | 2 ms
6,940 KB |
testcase_43 | AC | 2 ms
6,944 KB |
testcase_44 | AC | 2 ms
6,940 KB |
testcase_45 | AC | 101 ms
13,192 KB |
testcase_46 | AC | 98 ms
13,004 KB |
testcase_47 | AC | 94 ms
12,992 KB |
testcase_48 | AC | 96 ms
13,156 KB |
testcase_49 | AC | 92 ms
12,944 KB |
testcase_50 | AC | 95 ms
13,352 KB |
testcase_51 | AC | 95 ms
13,144 KB |
testcase_52 | AC | 92 ms
13,064 KB |
testcase_53 | AC | 94 ms
12,688 KB |
testcase_54 | AC | 100 ms
13,112 KB |
testcase_55 | AC | 97 ms
12,936 KB |
testcase_56 | AC | 104 ms
13,620 KB |
testcase_57 | AC | 97 ms
13,072 KB |
testcase_58 | AC | 98 ms
12,860 KB |
testcase_59 | AC | 110 ms
13,440 KB |
testcase_60 | AC | 106 ms
13,488 KB |
testcase_61 | AC | 107 ms
13,676 KB |
testcase_62 | AC | 92 ms
12,412 KB |
testcase_63 | AC | 100 ms
13,096 KB |
testcase_64 | AC | 99 ms
13,184 KB |
testcase_65 | AC | 15 ms
6,944 KB |
testcase_66 | AC | 15 ms
6,940 KB |
testcase_67 | AC | 94 ms
13,836 KB |
testcase_68 | AC | 96 ms
13,908 KB |
testcase_69 | AC | 71 ms
6,940 KB |
testcase_70 | AC | 65 ms
13,952 KB |
testcase_71 | AC | 42 ms
9,528 KB |
testcase_72 | AC | 56 ms
10,164 KB |
testcase_73 | AC | 57 ms
12,020 KB |
testcase_74 | AC | 55 ms
10,664 KB |
testcase_75 | AC | 84 ms
13,876 KB |
testcase_76 | AC | 80 ms
14,088 KB |
testcase_77 | AC | 71 ms
6,940 KB |
testcase_78 | AC | 74 ms
7,164 KB |
testcase_79 | AC | 59 ms
12,176 KB |
testcase_80 | AC | 60 ms
12,300 KB |
testcase_81 | AC | 50 ms
11,872 KB |
testcase_82 | AC | 51 ms
6,940 KB |
testcase_83 | AC | 55 ms
12,224 KB |
testcase_84 | AC | 55 ms
10,572 KB |
testcase_85 | AC | 41 ms
7,016 KB |
testcase_86 | AC | 36 ms
6,940 KB |
testcase_87 | AC | 73 ms
12,900 KB |
testcase_88 | AC | 68 ms
12,528 KB |
testcase_89 | AC | 1 ms
6,940 KB |
testcase_90 | AC | 1 ms
6,948 KB |
testcase_91 | AC | 1 ms
6,944 KB |
ソースコード
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; }