結果

問題 No.2449 square_permutation
ユーザー FromBooskaFromBooska
提出日時 2023-09-01 10:25:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,095 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 90,008 KB
最終ジャッジ日時 2023-09-01 10:25:38
合計ジャッジ時間 13,435 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
71,872 KB
testcase_01 AC 62 ms
71,740 KB
testcase_02 AC 65 ms
71,624 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 辞書順なので前から確定していくか
# idxに対して、一番大きくて平方数になる数を探して場所をスワップ
# nの倍数だけをサーチすると4と9が交換できず失敗する

N = int(input())
pos = [-1]*(N+1)
for n in range(1, N+1):
    if n > N/2 and pos[n] == -1:
        pos[n] = n
        continue
    if (n**0.5).is_integer() == True:
        # 倍数だけのサーチではダメだ、4が9と交換できない
        for j in range(N, n-1, -1):
            #print('n', n, 'j', j, (n*j)**0.5, ((n*j)**0.5).is_integer())
            if pos[j] == -1 and ((n*j)**0.5).is_integer() == True:
                pos[n] = j
                pos[j] = n
                break
        #print('n', n, 'pos', pos)
    else:
        mx = N-N%n
        for j in range(mx, n-1, -n):
            #print('n', n, 'j', j, (n*j)**0.5, ((n*j)**0.5).is_integer())
            if pos[j] == -1 and ((n*j)**0.5).is_integer() == True:
                pos[n] = j
                pos[j] = n
                break
        #print('n', n, 'pos', pos)        
print(*pos[1:])  
0