結果
問題 |
No.1854 Limited Bubble Sort
|
ユーザー |
|
提出日時 | 2022-02-24 23:25:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 97 ms / 2,000 ms |
コード長 | 1,135 bytes |
コンパイル時間 | 306 ms |
コンパイル使用メモリ | 82,944 KB |
実行使用メモリ | 83,840 KB |
最終ジャッジ日時 | 2024-07-02 14:15:01 |
合計ジャッジ時間 | 3,341 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 25 |
ソースコード
def yesno(p): for idx, (_, i) in enumerate(p): if idx == i and sum(j < i for _, j in p[:idx]) != i: return False return True def swap(p, i, ops, comp): if p[i][1] == i or p[i+1][1] == i+1: return if comp(p[i]) > comp(p[i+1]): p[i], p[i+1] = p[i+1], p[i] ops.append(i+1) def sort(p, ops, comp): for _ in p: for i in range(len(p)-1): swap(p, i, ops, comp) sum_n = 0 def solve(): global sum_n n = int(input()) sum_n += n assert 2 <= n <= 500 pp = list(map(int, input().split())) assert len(pp) == n assert list(sorted(pp)) == list(range(1, n + 1)) p = [(i-1 < idx, i-1) for idx, i in enumerate(pp)] if not yesno(p): print(-1) return ops = [] sort(p, ops, lambda x: x) sort(p, ops, lambda x: x[1]) print(len(ops)) print(" ".join(map(str, ops))) if __name__ == "__main__": t = int(input()) assert 1 <= t <= 250 for _ in range(t): solve() assert sum_n <= 500 try: x = input() assert x == "" except EOFError: pass