結果

問題 No.1635 Let’s Sort Integers!!
ユーザー Shirotsume
提出日時 2022-07-29 18:37:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 348 ms / 2,000 ms
コード長 2,778 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 223,160 KB
最終ジャッジ日時 2024-07-19 09:29:09
合計ジャッジ時間 19,783 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 77
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
from collections import deque
n, k = mi()

if n % 2:
    if n - 1 <= k <= (n * n - 3) // 2:
        pass
    else:
        exit(print(-1))
    if k <= n * (n - 1) // 2:
        q = deque(range(1, n + 1))
        ans = []
        ans.append(q.popleft())
        p = []
        f = 0
        for i in range(1, n):
            if i % 2:
                now = q.pop()
                if f + abs(ans[-1] - now) <= k:
                    f += abs(ans[-1] - now)
                    ans.append(now)
                else:
                    p.append(now)
            else:
                now = q.popleft()
                if f + abs(ans[-1] - now) <= k:
                    f += abs(ans[-1] - now)
                    ans.append(now)
                else:
                    p.append(now)
        ans = [ans[0]] + list(sorted(p)) + ans[1:]
    else:
        x = (n - 1) // 2 + 2
        y = 1
        ans = []
        for i in range(n - 3):
            if i % 2 == 0:
                ans.append(x)
                x += 1
            else:
                ans.append(y)
                y += 1

        ans.append(2 * ((n - 1) // 2) + 1)
        ans.append((n - 1) // 2)
        ans.append((n - 1) // 2 + 1)
        now1 = ans.index((n - 1) // 2 + 2)
        now2 = ans.index((n - 1) // 2 + 2 + (n * n - 3) // 2 - k)
        ans[now1], ans[now2] = ans[now2], ans[now1]
else:
    if n - 1 <= k <= (n * n) // 2 - 1:
        pass
    else:
        exit(print(-1))
    if k <= n * (n - 1) // 2:
        q = deque(range(1, n + 1))
        ans = []
        ans.append(q.popleft())
        p = []
        f = 0
        for i in range(1, n):
            if i % 2:
                now = q.pop()
                if f + abs(ans[-1] - now) <= k:
                    f += abs(ans[-1] - now)
                    ans.append(now)
                else:
                    p.append(now)
            else:
                now = q.popleft()
                if f + abs(ans[-1] - now) <= k:
                    f += abs(ans[-1] - now)
                    ans.append(now)
                else:
                    p.append(now)
        ans = [ans[0]] + list(sorted(p)) + ans[1:]
    else:
        x = n // 2 + 1
        y = 1
        ans = []
        for i in range(n):
            if i % 2:
                ans.append(y)
                y += 1
            else:
                ans.append(x)
                x += 1
        now1 = ans.index(n // 2 + 1)
        now2 = ans.index(n // 2 + 1 + (n * n) // 2 - 1 - k)
        ans[now1], ans[now2] = ans[now2], ans[now1]

cnt = 0
for i in range(n - 1):
    cnt += abs(ans[i] - ans[i + 1])
assert cnt == k

print(*ans)
0