結果

問題 No.3775 Distant Swap
コンテスト
ユーザー Rapca1256
提出日時 2026-07-27 14:03:25
言語 Python3
(3.15.2 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 99 ms / 2,000 ms
+ 898µs
コード長 1,450 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 52 ms
コンパイル使用メモリ 14,848 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2026-10-23 20:51:35
合計ジャッジ時間 3,494 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
import math
#from collections import deque, defaultdict, Counter
#import heapq
#import bisect
#import itertools
#import functools

# 外部ライブラリ(AtCoder環境で利用可能)
#from sortedcontainers import SortedList, SortedSet, SortedDict
#from atcoder.dsu import DSU
#from atcoder.segtree import SegTree
#from atcoder.lazysegtree import LazySegTree
#from atcoder.fenwicktree import FenwickTree

# 入力高速化
#input = sys.stdin.readline
# 再帰回数上限
sys.setrecursionlimit(10**9)

# よくあるmod
#MOD = 1000000007
MOD = 998244353

def bubble_sort(L):
    action = []
    goal = list(sorted(L))
    for i, n in enumerate(goal):
        cur = L.index(n)
        while i != cur:
            action.append((cur-1, cur))
            L[cur-1], L[cur] = L[cur], L[cur-1]
            cur -= 1

    return action
def solve():
    # 解答ここから
    import heapq
    N, K = map(int, input().split(' '))
    A = list(map(int, input().split(' ')))
    sp = [[] for _ in range(K)]
    for i in range(N):
        if A[i] % K != (i+1) % K:
            print(-1)
            return
        sp[(i+1) % K].append(A[i])
    
    answer = []
    for k in range(1, K+1):
        actions = bubble_sort(sp[k % K])
        for i, j in actions:
            answer.append((K*i+k, K*j+k))
    print(len(answer))
    for i, j in answer:
        print(i, j)
if __name__ == '__main__':
    T = 1
    for _ in range(T):
        solve()
0