結果

問題 No.1369 交換門松列・竹
ユーザー mai
提出日時 2020-11-03 16:04:45
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 1,388 bytes
コンパイル時間 42 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 26,368 KB
最終ジャッジ日時 2024-07-22 09:21:18
合計ジャッジ時間 23,033 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28 WA * 2 TLE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def 🎍(a,b,c)
    a != c && ((a < b && b > c) || (a > b && b < c))
end

# - - - - -

def solve(n, arr)
    
    not_kado_idxs = arr.each_cons(3).each_with_index.map{|a,i| 🎍(*a) ? nil : i}.compact
    
    return false if not_kado_idxs.size > 20
    
    validate_idxs = not_kado_idxs.map{|i| (-1..0).map{|e|i+e}.to_a}.flatten.uniq.select{|i| 0 <= i && i < n-2}
    
    ok = false
    n.times do |i|
        not_kado_idxs.each do |ji|
            3.times do |jj|
                j = ji+jj
                next if i == j
                arr[i], arr[j] = arr[j], arr[i]
                ok ||= validate_idxs.each.all?{|x| 🎍(arr[x], arr[x+1], arr[x+2])} &&
                       (-2..0).map{|e|i+e}.select{|i| 0 <= i && i < n-2}.all?{|x| 🎍(arr[x], arr[x+1], arr[x+2])}
                arr[i], arr[j] = arr[j], arr[i]
            end
        end
    end
    ok
end



line1 = gets.chomp!
abort unless line1 =~ /^\d+$/

t = line1.to_i

t.times do |i|
  linea = gets.chomp!
  lineb = gets.chomp!
  abort "#{i}: n format" unless linea =~ /^\d+$/
  abort "#{i}: A format" unless lineb =~ /^\d+(?: \d+)+$/
  
  n = linea.to_i
  arr = lineb.split.map(&:to_i)
  
  abort "#{i}: n"    unless n == arr.size 
  abort "#{i}: A[i]" unless arr.all?{|a| 1 <= a && a <= n}
  abort "#{i}: 🎍" if arr.each_cons(3).all?{|a| 🎍(*a)}
  puts solve(n, arr) ? 'Yes' : 'No'
end
abort 'break' if gets

0