結果

問題 No.429 CupShuffle
ユーザー gemmarogemmaro
提出日時 2020-07-19 09:06:29
言語 Ruby
(3.3.0)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 1,014 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 11,484 KB
実行使用メモリ 45,400 KB
最終ジャッジ日時 2023-08-22 09:45:23
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,264 KB
testcase_01 AC 82 ms
15,044 KB
testcase_02 AC 83 ms
15,152 KB
testcase_03 AC 81 ms
15,016 KB
testcase_04 AC 84 ms
15,156 KB
testcase_05 AC 83 ms
15,256 KB
testcase_06 AC 85 ms
15,124 KB
testcase_07 AC 84 ms
15,100 KB
testcase_08 AC 81 ms
15,344 KB
testcase_09 AC 86 ms
15,344 KB
testcase_10 AC 114 ms
18,332 KB
testcase_11 AC 424 ms
45,400 KB
testcase_12 AC 431 ms
45,064 KB
testcase_13 AC 449 ms
44,928 KB
testcase_14 AC 450 ms
44,832 KB
testcase_15 AC 82 ms
15,040 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# frozen_string_literal: true

class Array
  def swap!(a, b)
    ia = a
    ib = b
    self[ia], self[ib] = self[ib], self[ia]
  end

  def divide_by_question
    question_index = index { _1[0] == '?' }
    if question_index.zero?
      [[], to_integer]
    else
      [self[0..(question_index - 1)], self[(question_index + 1)..-1]]
        .map { _1.to_integer }
    end
  end

  def to_integer
    map { _1.map(&:to_i) }
  end

  def move_cups(moves)
    moves.each do |left, right|
      swap!(left - 1, right - 1)
    end

    self
  end

  def detect_position(other)
    zip(other)
      .map
      .with_index { |pair, index| [pair[0] != pair[1], index] }
      .filter { |flag, _index| flag }
      .map { |_flag, index| index + 1 }
      .join(' ')
  end
end

def solve
  t, b = A.divide_by_question
  ct = (1..N).to_a.move_cups(t)
  cb = C.move_cups(b.reverse)
  ct.detect_position(cb)
end

N, K, X = gets.split.map(&:to_i)
A = K.times.map { gets.chomp.split }
C = gets.chomp.split.map(&:to_i)

puts solve
0