結果

問題 No.566 だいたい完全二分木
ユーザー sho_00sho_00
提出日時 2020-04-12 16:05:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 827 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 66,388 KB
最終ジャッジ日時 2023-10-22 01:20:10
合計ジャッジ時間 2,026 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,608 KB
testcase_01 AC 40 ms
53,608 KB
testcase_02 AC 40 ms
53,608 KB
testcase_03 AC 40 ms
53,608 KB
testcase_04 AC 40 ms
53,608 KB
testcase_05 AC 40 ms
53,608 KB
testcase_06 AC 41 ms
53,608 KB
testcase_07 AC 42 ms
53,608 KB
testcase_08 AC 43 ms
53,608 KB
testcase_09 AC 49 ms
61,312 KB
testcase_10 AC 59 ms
66,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math,sys
K=int(input())
v=2**(K-1)+1
P=[str(v)]
left=[str(i) for i in range(1,v)]
right=[str(i) for i in range(v+1,2**K)]

def f(list):
  if len(list)==0:
    return []
  elif len(list)==1:
    return [list[0]]
  else:
    index=len(list)//2
    return [list[index],list[0:index],list[index+1:]]

que=[left,right]
while len(P)!=2**K-1:
  if len(que)<=1:
    print('error')
  else:
    l=que.pop(0)
    r=que.pop(0)
    tmp1=f(l)
    tmp2=f(r)
    if len(tmp1)==3:
      v1,l1,r1=tmp1[0],tmp1[1],tmp1[2]
      P.append(str(v1))
      que.append(l1)
      que.append(r1)
    if len(tmp2)==3:
      v2,l2,r2=tmp2[0],tmp2[1],tmp2[2]
      P.append(str(v2))
      que.append(l2)
      que.append(r2)
    if len(tmp1)==1:
      P.append(str(tmp1[0]))
    if len(tmp2)==1:
      P.append(str(tmp2[0]))
      
print(' '.join(P))
0