結果
| 問題 |
No.3047 Verification of Sorting Network
|
| ユーザー |
👑 |
| 提出日時 | 2025-02-10 15:36:41 |
| 言語 | Ruby (3.4.1) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,516 bytes |
| コンパイル時間 | 398 ms |
| コンパイル使用メモリ | 8,736 KB |
| 実行使用メモリ | 43,868 KB |
| 最終ジャッジ日時 | 2025-03-05 20:39:53 |
| 合計ジャッジ時間 | 90,374 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 47 TLE * 14 |
コンパイルメッセージ
Syntax OK
ソースコード
#!/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
unused = Array.new(m, true)
unsorted = Array.new(n - 1, false)
pbits = 15
pfull = (1 << (1 << pbits)) - 1
lows = Array.new(pbits, 0)
pbits.times do |i|
le = ((1 << (1 << i)) - 1) << (1 << i)
(pbits - i - 1).times do |j|
le |= (le << (2 << (i + j)))
end
lows[i] = le
end
(1 << [n - pbits, 0].max).times do |i|
p = lows.dup
(n - pbits).times do |j|
p.push(((i >> j) & 1) == 1 ? pfull : 0)
end
m.times do |j|
a, b = cmps[j]
pa, pb = p[a], p[b]
na = (pa & pb)
if pa != na
p[a], p[b], unused[j] = na, (pa | pb), false
end
end
(n - 1).times do |j|
unsorted[j] = true if (p[j] & ~p[j + 1]) != 0
end
end
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