結果

問題 No.1369 交換門松列・竹
ユーザー maimai
提出日時 2020-11-03 17:14:02
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,679 ms / 2,000 ms
コード長 1,461 bytes
コンパイル時間 52 ms
コンパイル使用メモリ 11,688 KB
実行使用メモリ 27,468 KB
最終ジャッジ日時 2023-09-29 15:12:39
合計ジャッジ時間 18,119 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
15,228 KB
testcase_01 AC 72 ms
15,228 KB
testcase_02 AC 71 ms
15,160 KB
testcase_03 AC 67 ms
15,116 KB
testcase_04 AC 68 ms
15,228 KB
testcase_05 AC 68 ms
15,200 KB
testcase_06 AC 70 ms
15,156 KB
testcase_07 AC 72 ms
15,228 KB
testcase_08 AC 70 ms
15,196 KB
testcase_09 AC 70 ms
15,232 KB
testcase_10 AC 68 ms
15,200 KB
testcase_11 AC 68 ms
15,228 KB
testcase_12 AC 69 ms
15,232 KB
testcase_13 AC 202 ms
15,400 KB
testcase_14 AC 297 ms
15,484 KB
testcase_15 AC 258 ms
26,844 KB
testcase_16 AC 551 ms
26,888 KB
testcase_17 AC 841 ms
27,060 KB
testcase_18 AC 599 ms
26,932 KB
testcase_19 AC 263 ms
26,936 KB
testcase_20 AC 1,425 ms
26,928 KB
testcase_21 AC 258 ms
26,796 KB
testcase_22 AC 1,302 ms
27,116 KB
testcase_23 AC 601 ms
26,872 KB
testcase_24 AC 1,679 ms
27,000 KB
testcase_25 AC 1,324 ms
16,040 KB
testcase_26 AC 1,189 ms
15,952 KB
testcase_27 AC 1,141 ms
16,220 KB
testcase_28 AC 1,335 ms
20,484 KB
testcase_29 AC 1,636 ms
20,508 KB
testcase_30 AC 109 ms
27,468 KB
testcase_31 AC 108 ms
26,080 KB
testcase_32 AC 138 ms
15,496 KB
testcase_33 AC 619 ms
20,848 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
    
    not_kado_elem_idxs = not_kado_idxs.map{|i| [i, i+1, i+2]}.flatten.uniq
    validate_idxs = not_kado_idxs.map{|i| (-3..2).map{|e|i+e}.to_a}.flatten.uniq.select{|i| 0 <= i && i < n-2}
    
    ok = false
    n.times do |i|
        ii = (-3..2).map{|e|i+e}.select{|i| 0 <= i && i < n-2}
        not_kado_elem_idxs.each do |j|
            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])} &&
                   ii.all?{|x| 🎍(arr[x], arr[x+1], arr[x+2])}
            arr[i], arr[j] = arr[j], arr[i]
        end
    end
    ok
end



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

t = line1.to_i

n_total = 0

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)
  n_total += n
  
  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

abort 'n_total' unless n_total <= 50000
0