結果

問題 No.1606 Stuffed Animals Keeper
ユーザー yuruhiyayuruhiya
提出日時 2021-07-17 13:12:13
言語 Crystal
(1.11.2)
結果
AC  
実行時間 140 ms / 3,000 ms
コード長 3,054 bytes
コンパイル時間 21,263 ms
コンパイル使用メモリ 252,212 KB
実行使用メモリ 5,204 KB
最終ジャッジ日時 2023-09-21 06:30:26
合計ジャッジ時間 25,497 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,500 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,520 KB
testcase_03 AC 61 ms
4,820 KB
testcase_04 AC 75 ms
5,008 KB
testcase_05 AC 16 ms
5,072 KB
testcase_06 AC 27 ms
5,068 KB
testcase_07 AC 52 ms
5,032 KB
testcase_08 AC 47 ms
4,944 KB
testcase_09 AC 22 ms
4,928 KB
testcase_10 AC 13 ms
5,188 KB
testcase_11 AC 7 ms
5,080 KB
testcase_12 AC 27 ms
5,084 KB
testcase_13 AC 3 ms
4,400 KB
testcase_14 AC 3 ms
4,576 KB
testcase_15 AC 3 ms
4,552 KB
testcase_16 AC 95 ms
4,920 KB
testcase_17 AC 97 ms
5,092 KB
testcase_18 AC 98 ms
5,072 KB
testcase_19 AC 98 ms
5,008 KB
testcase_20 AC 96 ms
4,956 KB
testcase_21 AC 18 ms
5,044 KB
testcase_22 AC 16 ms
4,960 KB
testcase_23 AC 16 ms
5,060 KB
testcase_24 AC 18 ms
5,088 KB
testcase_25 AC 16 ms
4,860 KB
testcase_26 AC 8 ms
5,132 KB
testcase_27 AC 8 ms
5,080 KB
testcase_28 AC 7 ms
4,908 KB
testcase_29 AC 9 ms
5,132 KB
testcase_30 AC 7 ms
5,072 KB
testcase_31 AC 6 ms
5,204 KB
testcase_32 AC 3 ms
4,724 KB
testcase_33 AC 5 ms
5,056 KB
testcase_34 AC 4 ms
4,920 KB
testcase_35 AC 6 ms
4,944 KB
testcase_36 AC 5 ms
4,912 KB
testcase_37 AC 5 ms
5,036 KB
testcase_38 AC 5 ms
5,048 KB
testcase_39 AC 4 ms
4,816 KB
testcase_40 AC 4 ms
4,796 KB
testcase_41 AC 3 ms
4,620 KB
testcase_42 AC 3 ms
4,428 KB
testcase_43 AC 140 ms
4,888 KB
testcase_44 AC 138 ms
5,020 KB
testcase_45 AC 138 ms
5,044 KB
testcase_46 AC 139 ms
5,004 KB
testcase_47 AC 138 ms
5,024 KB
testcase_48 AC 3 ms
4,412 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 3 ms
4,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "/scanner"
class Scanner
  private def self.skip_to_not_space
    peek = STDIN.peek
    not_space = peek.index { |x| x != 32 && x != 10 } || peek.size
    STDIN.skip(not_space)
  end

  def self.c
    skip_to_not_space
    STDIN.read_char.not_nil!
  end

  def self.s
    skip_to_not_space

    peek = STDIN.peek
    if index = peek.index { |x| x == 32 || x == 10 }
      STDIN.skip(index + 1)
      return String.new(peek[0, index])
    end

    String.build do |buffer|
      loop do
        buffer.write peek
        STDIN.skip(peek.size)
        peek = STDIN.peek
        break if peek.empty?
        if index = peek.index { |x| x == 32 || x == 10 }
          buffer.write peek[0, index]
          STDIN.skip(index)
          break
        end
      end
    end
  end
end

macro internal_input(s, else_ast)
  {% if Scanner.class.has_method?(s.id) %}
    Scanner.{{s.id}}
  {% elsif s.stringify == "String" %}
    Scanner.s
  {% elsif s.stringify == "Char" %}
    Scanner.c
  {% elsif s.stringify =~ /[A-Z][a-z0-9_]*/ %}
    {{s.id}}.new(Scanner.s)
  {% elsif String.has_method?("to_#{s}".id) %}
    Scanner.s.to_{{s.id}}
  {% else %}
    {{else_ast}}
  {% end %}
end

macro internal_input_array(s, args, else_ast)
  {% if Scanner.class.has_method?(s.id) ||
          s.stringify =~ /[A-Z][a-z0-9_]*/ ||
          String.has_method?("to_#{s}".id) %}
    Array.new({{args.first}}) do
      {% if args.size == 1 %}
        input({{s.id}})
      {% else %}
        internal_input_array({{s}}, {{args[1...args.size]}}, else_ast)
      {% end %}
    end
  {% else %}
    {{else_ast}}
  {% end %}
end

macro input(s)
  {% if s.is_a?(Call) %}
    {% if s.receiver.is_a?(Nop) %}
      internal_input(
        {{s.name}}, {{s.name}}(
          {% for argument in s.args %}
            input({{argument}}),
          {% end %}
        )
      )
    {% elsif s.name.stringify == "[]" %}
      internal_input_array(
        {{s.receiver}}, {{s.args}}, {{s.receiver}}[
          {% for argument in s.args %}
            input({{argument}}),
          {% end %}
        ] {{s.block}}
      )
    {% else %}
      input({{s.receiver}}).{{s.name.id}}(
        {% for argument in s.args %}
          input({{argument}}),
        {% end %}
      ) {{s.block}}
    {% end %}
  {% else %}
    internal_input({{s.id}}, {{s.id}})
  {% end %}
end

macro input(*s)
  {
    {% for s in s %}
      input({{s}}),
    {% end %}
  }
end

# require "/array/change"
class Array(T)
  def chmin(i : Int, value : T)
    (self[i] > value).tap do |f|
      self[i] = value if f
    end
  end

  def chmax(i : Int, value : T)
    (self[i] < value).tap do |f|
      self[i] = value if f
    end
  end
end

n = input(i)
s = input(c[n].join)
cnt = s.split('2', remove_empty: true).map { |s| {s.count('0'), s.count('1')} }

ans = cnt.reduce(
  [0] + [10**9] * (2*n)
) { |dp, (a, b)|
  (-n..n).each_with_object([10**9] * (2*n + 1)) do |i, dp2|
    dp2.chmin(i - a, dp[i] + a) if i - a >= -n
    dp2.chmin(i + b, dp[i] + b) if i + b <= +n
  end
}[0]

puts ans < 10**9 ? ans // 2 : -1
0