結果

問題 No.738 平らな農地
ユーザー te-shte-sh
提出日時 2020-01-29 16:43:51
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 4,248 bytes
コンパイル時間 1,755 ms
コンパイル使用メモリ 157,464 KB
実行使用メモリ 15,140 KB
最終ジャッジ日時 2023-09-04 05:19:06
合計ジャッジ時間 10,820 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 4 ms
4,376 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 1 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 4 ms
4,384 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 84 ms
12,500 KB
testcase_16 AC 102 ms
12,208 KB
testcase_17 AC 116 ms
11,704 KB
testcase_18 AC 92 ms
10,748 KB
testcase_19 AC 143 ms
11,720 KB
testcase_20 AC 88 ms
12,452 KB
testcase_21 AC 103 ms
12,808 KB
testcase_22 AC 89 ms
11,560 KB
testcase_23 AC 128 ms
11,668 KB
testcase_24 AC 103 ms
12,172 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 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,376 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 3 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,380 KB
testcase_41 AC 2 ms
4,384 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 91 ms
12,104 KB
testcase_46 AC 101 ms
11,880 KB
testcase_47 AC 90 ms
11,800 KB
testcase_48 AC 96 ms
13,152 KB
testcase_49 AC 92 ms
11,676 KB
testcase_50 AC 95 ms
13,228 KB
testcase_51 AC 93 ms
11,932 KB
testcase_52 AC 84 ms
11,444 KB
testcase_53 AC 87 ms
12,264 KB
testcase_54 AC 90 ms
12,588 KB
testcase_55 AC 95 ms
13,436 KB
testcase_56 AC 109 ms
12,912 KB
testcase_57 AC 97 ms
11,884 KB
testcase_58 AC 87 ms
12,024 KB
testcase_59 AC 105 ms
11,964 KB
testcase_60 AC 105 ms
12,112 KB
testcase_61 AC 102 ms
13,408 KB
testcase_62 AC 80 ms
11,992 KB
testcase_63 AC 96 ms
12,324 KB
testcase_64 AC 92 ms
12,812 KB
testcase_65 AC 117 ms
11,884 KB
testcase_66 AC 125 ms
11,928 KB
testcase_67 AC 83 ms
13,888 KB
testcase_68 AC 99 ms
14,856 KB
testcase_69 AC 137 ms
13,996 KB
testcase_70 AC 114 ms
14,588 KB
testcase_71 AC 25 ms
5,752 KB
testcase_72 AC 45 ms
10,536 KB
testcase_73 AC 40 ms
9,300 KB
testcase_74 AC 46 ms
10,396 KB
testcase_75 AC 140 ms
14,616 KB
testcase_76 AC 122 ms
15,124 KB
testcase_77 AC 150 ms
13,736 KB
testcase_78 AC 125 ms
14,424 KB
testcase_79 AC 123 ms
14,360 KB
testcase_80 AC 104 ms
13,980 KB
testcase_81 AC 108 ms
15,140 KB
testcase_82 AC 156 ms
13,996 KB
testcase_83 AC 37 ms
7,928 KB
testcase_84 AC 39 ms
7,676 KB
testcase_85 AC 201 ms
14,276 KB
testcase_86 AC 134 ms
13,456 KB
testcase_87 AC 72 ms
13,524 KB
testcase_88 AC 69 ms
14,932 KB
testcase_89 AC 1 ms
4,380 KB
testcase_90 AC 2 ms
4,380 KB
testcase_91 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/738

import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int N, K; io.getV(N, K);
  int[] A; io.getA(N, A);

  auto za = Zaatsu!int(A);
  auto c = za.comp(A).array;

  auto fc = fenwickTree!int(za.n), fs = fenwickTree!long(za.n);
  foreach (i; 0..K) {
    fc[c[i]] += 1;
    fs[c[i]] += za.uncomp(c[i]);
  }

  auto ans = 10L^^18;
  foreach (i; 0..N-K+1) {
    auto s = iota(0, za.n).map!(j => fc[0..j]).assumeSorted;
    if (K%2 == 0) {
      auto j1 = cast(int)s.lowerBound(K/2).length-1, j2 = cast(int)s.lowerBound(K/2+1).length-1;
      auto f1 = fs[j2..$]-cast(long)za.uncomp(j2)*(fc[j2..$]-K/2);
      auto f2 = fs[0..j1+1]-cast(long)za.uncomp(j1)*(fc[0..j1+1]-K/2);
      ans = min(ans, f1-f2);
    } else {
      auto j = cast(int)s.lowerBound(K/2+1).length-1;
      auto f1 = fs[j..$]-cast(long)za.uncomp(j)*(fc[j..$]-K/2);
      auto f2 = fs[0..j+1]-cast(long)za.uncomp(j)*(fc[0..j+1]-K/2);
      ans = min(ans, f1-f2);
    }

    if (i < N-K) {
      fc[c[i]] -= 1;
      fs[c[i]] -= za.uncomp(c[i]);
      fc[c[i+K]] += 1;
      fs[c[i+K]] += za.uncomp(c[i+K]);
    }
  }

  io.put(ans);
}

struct Zaatsu(T)
{
  const size_t n;

  this(U...)(U v)
  {
    T[] d;
    foreach (w; v) d ~= w.array;
    auto u = d.sort.uniq;
    n = u.walkLength;
    c2 = new T[](n);

    foreach (i, ui; u.enumerate(0)) {
      c1[ui] = i;
      c2[i] = ui;
    }
  }

  int comp(T v) { return c1[v]; }
  auto comp(R)(R v) if (isInputRange!R) { return v.map!(w => c1[w]); }

  T uncomp(int v) { return c2[v]; }
  auto uncomp(R)(R v) if (isInputRange!R) { return v.map!(w => c2[w]); }

  private
  {
    int[T] c1;
    T[] c2;
  }
}

class FenwickTree(T)
{
  const size_t n;

  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(op~"buf[i];"); }

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

  private
  {
    T[] buf;

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

auto io = IO!()();
import std.stdio;
struct IO(string floatFormat = "%.10f", string delimiter = " ", alias IN = stdin, alias OUT = stdout)
{
  import std.conv, std.format, std.meta, std.traits;

  auto getV(T...)(ref T v) { foreach (ref w; v) get(w); }
  auto getA(T)(size_t n, ref T v) if (hasAssignableElements!T)
  { v = new T(n); foreach (ref w; v) get(w); }
  auto getC(T...)(size_t n, ref T v)
  if (allSatisfy!(hasAssignableElements, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  auto getM(T)(size_t r, size_t c, ref T v)
  if (hasAssignableElements!T && hasAssignableElements!(ElementType!T))
  {
    v = new T(r); foreach (ref w; v) getA(c, w);
  }
  template getS(E...)
  {
    auto getS(T)(size_t n, ref T v)
    {
      v = new T(n); foreach (ref w; v) foreach (e; E) mixin("get(w."~e~");");
    }
  }

  auto put(bool flush = false, T...)(T v)
  {
    foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); }
    OUT.writeln;
    static if (flush) OUT.flush();
  }
  auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); }
  auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; }

  private
  {
    dchar[] buf;
    auto sp = (new dchar[](0)).splitter;
    void nextLine() { IN.readln(buf); sp = buf.splitter; }
    auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); }

    auto putR(T)(T v)
    {
      auto w = v;
      while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); }
    }
    auto putA(T)(T v)
    {
      static if (isInputRange!T && !isSomeString!T) putR(v);
      else if (isFloatingPoint!T) OUT.write(format(floatFormat, v));
      else OUT.write(v);
    }
  }
}
0