結果

問題 No.738 平らな農地
ユーザー te-shte-sh
提出日時 2018-10-10 15:57:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 2,042 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 103,880 KB
実行使用メモリ 14,224 KB
最終ジャッジ日時 2023-09-03 21:11:59
合計ジャッジ時間 9,431 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 5 ms
4,384 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 2 ms
4,504 KB
testcase_15 AC 81 ms
11,156 KB
testcase_16 AC 104 ms
11,684 KB
testcase_17 AC 118 ms
11,152 KB
testcase_18 AC 90 ms
9,456 KB
testcase_19 AC 142 ms
10,820 KB
testcase_20 AC 84 ms
12,168 KB
testcase_21 AC 98 ms
10,384 KB
testcase_22 AC 86 ms
10,304 KB
testcase_23 AC 129 ms
9,316 KB
testcase_24 AC 103 ms
9,212 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 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 87 ms
11,572 KB
testcase_46 AC 103 ms
9,108 KB
testcase_47 AC 88 ms
12,296 KB
testcase_48 AC 92 ms
9,688 KB
testcase_49 AC 92 ms
9,108 KB
testcase_50 AC 93 ms
10,360 KB
testcase_51 AC 90 ms
12,684 KB
testcase_52 AC 80 ms
10,164 KB
testcase_53 AC 79 ms
9,556 KB
testcase_54 AC 86 ms
9,732 KB
testcase_55 AC 89 ms
9,716 KB
testcase_56 AC 105 ms
10,364 KB
testcase_57 AC 98 ms
9,784 KB
testcase_58 AC 82 ms
11,756 KB
testcase_59 AC 101 ms
9,816 KB
testcase_60 AC 103 ms
9,800 KB
testcase_61 AC 100 ms
10,808 KB
testcase_62 AC 78 ms
9,188 KB
testcase_63 AC 91 ms
11,776 KB
testcase_64 AC 88 ms
9,988 KB
testcase_65 AC 116 ms
9,188 KB
testcase_66 AC 127 ms
9,796 KB
testcase_67 AC 74 ms
11,240 KB
testcase_68 AC 90 ms
12,572 KB
testcase_69 AC 129 ms
11,124 KB
testcase_70 AC 107 ms
12,352 KB
testcase_71 AC 20 ms
4,380 KB
testcase_72 AC 29 ms
6,512 KB
testcase_73 AC 26 ms
5,892 KB
testcase_74 AC 29 ms
5,896 KB
testcase_75 AC 134 ms
10,592 KB
testcase_76 AC 114 ms
12,552 KB
testcase_77 AC 142 ms
10,472 KB
testcase_78 AC 117 ms
12,712 KB
testcase_79 AC 114 ms
12,668 KB
testcase_80 AC 95 ms
10,880 KB
testcase_81 AC 102 ms
10,736 KB
testcase_82 AC 151 ms
12,580 KB
testcase_83 AC 25 ms
6,096 KB
testcase_84 AC 25 ms
5,776 KB
testcase_85 AC 200 ms
14,224 KB
testcase_86 AC 129 ms
10,232 KB
testcase_87 AC 64 ms
10,704 KB
testcase_88 AC 62 ms
12,376 KB
testcase_89 AC 1 ms
4,376 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, k; readV(n, k);
  int[] a; readA(n, a);

  int[int] z;
  auto zn = 0, b = a.dup.sort().uniq.array;
  foreach (bi; b) z[bi] = zn++;
  auto c = a.map!(ai => z[ai]).array;

  auto fc = new FenwickTree!int(zn), fs = new FenwickTree!long(zn);
  foreach (i; 0..k) {
    fc[c[i]] += 1;
    fs[c[i]] += b[c[i]];
  }

  auto ans = 10L^^18;
  foreach (i; 0..n-k+1) {
    auto s = iota(0, zn).map!(j => fc[0..j]).assumeSorted;
    if (k%2 == 0) {
      auto j1 = s.lowerBound(k/2).length-1, j2 = s.lowerBound(k/2+1).length-1;
      ans = min(ans, (fs[j2..$]-b[j2].to!long*(fc[j2..$]-k/2))-(fs[0..j1+1]-b[j1].to!long*(fc[0..j1+1]-k/2)));
    } else {
      auto j = s.lowerBound(k/2+1).length-1;
      ans = min(ans, (fs[j..$]-b[j].to!long*(fc[j..$]-k/2))-(fs[0..j+1]-b[j].to!long*(fc[0..j+1]-k/2)));
    }

    if (i < n-k) {
      fc[c[i]] -= 1;
      fs[c[i]] -= b[c[i]];
      fc[c[i+k]] += 1;
      fs[c[i+k]] += b[c[i+k]];
    }
  }

  writeln(ans);
}

class FenwickTree(T)
{
  const size_t n;
  T[] buf;

  this(size_t n)
  {
    this.n = n;
    this.buf = new T[](n+1);
  }

  void opIndexOpAssign(string op)(T val, size_t i) if (op == "+" || op == "-")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
  }

  void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
  {
    ++i;
    for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
  }

  pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
  pure T opIndex(size_t i) { return opSlice(i, i+1); }
  pure size_t opDollar() { return n; }

private:

  pure T get(size_t i)
  {
    auto s = T(0);
    for (; i > 0; i -= i & -i) s += buf[i];
    return s;
  }
}
0