結果

問題 No.738 平らな農地
ユーザー te-shte-sh
提出日時 2020-01-29 16:43:51
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 4,248 bytes
コンパイル時間 1,803 ms
コンパイル使用メモリ 159,456 KB
実行使用メモリ 15,172 KB
最終ジャッジ日時 2024-06-22 04:45:47
合計ジャッジ時間 9,695 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 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 4 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 78 ms
10,788 KB
testcase_16 AC 95 ms
12,396 KB
testcase_17 AC 109 ms
10,928 KB
testcase_18 AC 86 ms
10,360 KB
testcase_19 AC 134 ms
11,360 KB
testcase_20 AC 80 ms
11,184 KB
testcase_21 AC 95 ms
12,352 KB
testcase_22 AC 81 ms
11,508 KB
testcase_23 AC 122 ms
11,224 KB
testcase_24 AC 98 ms
11,496 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,948 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 1 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 86 ms
12,892 KB
testcase_46 AC 98 ms
11,332 KB
testcase_47 AC 83 ms
12,340 KB
testcase_48 AC 86 ms
11,628 KB
testcase_49 AC 83 ms
11,420 KB
testcase_50 AC 90 ms
11,736 KB
testcase_51 AC 86 ms
11,616 KB
testcase_52 AC 80 ms
12,872 KB
testcase_53 AC 78 ms
11,804 KB
testcase_54 AC 83 ms
13,248 KB
testcase_55 AC 86 ms
12,064 KB
testcase_56 AC 99 ms
11,416 KB
testcase_57 AC 93 ms
12,100 KB
testcase_58 AC 79 ms
12,544 KB
testcase_59 AC 99 ms
12,620 KB
testcase_60 AC 99 ms
11,872 KB
testcase_61 AC 97 ms
11,980 KB
testcase_62 AC 76 ms
11,528 KB
testcase_63 AC 87 ms
12,060 KB
testcase_64 AC 85 ms
12,092 KB
testcase_65 AC 109 ms
10,560 KB
testcase_66 AC 115 ms
11,744 KB
testcase_67 AC 78 ms
14,328 KB
testcase_68 AC 98 ms
13,484 KB
testcase_69 AC 129 ms
14,884 KB
testcase_70 AC 110 ms
14,036 KB
testcase_71 AC 21 ms
6,944 KB
testcase_72 AC 40 ms
10,388 KB
testcase_73 AC 39 ms
8,756 KB
testcase_74 AC 40 ms
10,728 KB
testcase_75 AC 131 ms
15,172 KB
testcase_76 AC 113 ms
13,356 KB
testcase_77 AC 138 ms
13,028 KB
testcase_78 AC 125 ms
13,804 KB
testcase_79 AC 113 ms
13,860 KB
testcase_80 AC 97 ms
13,648 KB
testcase_81 AC 99 ms
13,192 KB
testcase_82 AC 150 ms
14,812 KB
testcase_83 AC 35 ms
8,580 KB
testcase_84 AC 36 ms
7,336 KB
testcase_85 AC 190 ms
13,564 KB
testcase_86 AC 121 ms
12,768 KB
testcase_87 AC 65 ms
13,460 KB
testcase_88 AC 61 ms
13,004 KB
testcase_89 AC 1 ms
6,944 KB
testcase_90 AC 1 ms
6,940 KB
testcase_91 AC 1 ms
6,940 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