結果

問題 No.3047 Verification of Sorting Network
ユーザー 👑 Mizar
提出日時 2025-01-17 23:56:20
言語 Ruby
(3.4.1)
結果
AC  
実行時間 1,831 ms / 2,000 ms
コード長 3,018 bytes
コンパイル時間 73 ms
コンパイル使用メモリ 8,480 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2025-03-05 20:32:31
合計ジャッジ時間 79,926 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/env ruby

MAX_T = 10000
MAX_N = 27
MAX_COST = 1e8
PHI = Math.sqrt(1.25) + 0.5  # 1.61803398875, golden ratio

class IsSortingOk
  def initialize data
    @data = data
  end
  def is_sorting?
    true
  end
  def to_s
    "Yes"
  end
  def data
    @data
  end
end

class IsSortingNg
  def initialize data
    @data = data
  end
  def is_sorting?
    false
  end
  def to_s
    "No"
  end
  def data
    @data
  end
end

def is_sorting_network(n, cmps)
  raise "Invalid input" unless n >= 2 && cmps.all? { |a, b| 0 <= a && a < b && b < n }
  m = cmps.length
  stack = [[0, [2]*n, 0, n - 1]]
  unused = Array.new(m, true)
  unsorted = Array.new(n - 1, false)
  until stack.empty?
    catch do |sortedtag|
      i, p, z, o = stack.pop
      while i < m
        a, b = cmps[i]
        if p[a] != 0 and p[b] != 1
          unused[i] = false
          if p[a] === 2 and p[b] === 2
            q = p.dup
            q[a], q[b], p[b] = 0, 0, 1
            j = z
            for j in z...o
              if q[j] != 0
                stack << [i, q, j, o]
                break
              end
            end
            while p[o] === 1 
              o -= 1
              if z === o
                throw sortedtag
              end
            end
          else
            p[a], p[b] = p[b], p[a]
            while p[z] === 0 
              z += 1
              if z === o
                throw sortedtag
              end
            end
            while p[o] === 1
              o -= 1
              if z === o
                throw sortedtag
              end
            end
          end
        end
        i += 1
      end
      for j in 0..n - 2
        if p[j] != 0 and p[j + 1] != 1
          unsorted[j] = true
        end
      end
    end  # catch
  end  # until
  if unsorted.any?
    return IsSortingNg.new(unsorted)
  end
  return IsSortingOk.new(unused)
end

def main
  RubyVM::YJIT.enable

  # cost of the test cases
  cost = 0
  # number of test cases
  t = gets.to_i
  raise "Invalid input" unless (1..MAX_T).include?(t)

  t.times do
    # number of elements, number of comparisons
    n, m = gets.split.map(&:to_i)
    raise "Invalid input" unless (2..MAX_N).include?(n) && (1..(n * (n - 1) / 2)).include?(m)
    cost += m * PHI**n
    raise "Cost too high" if cost > MAX_COST
    # 1-indexed -> 0-indexed
    va = gets.split.map {|x| x.to_i - 1 }
    vb = gets.split.map {|x| x.to_i - 1 }
    cmps = va.zip(vb)
    cmps.each do |a, b|
      raise "Invalid input" unless 0 <= a && a < b && b < n
    end
    result = is_sorting_network(n, cmps)
    puts result
    if result.is_sorting?
      raise "Invalid output" unless result.data.length == m
      puts result.data.count { |x| x }
      puts result.data.each_with_index.filter_map { |x, i| i + 1 if x }.join(' ')
    else
      raise "Invalid output" unless result.data.length == n - 1
      puts result.data.count { |x| x }
      puts result.data.each_with_index.filter_map { |x, i| i + 1 if x }.join(' ')
    end
  end
end

main
0