結果

問題 No.1604 Swap Sort:ONE
ユーザー 小野寺健小野寺健
提出日時 2021-12-17 13:31:38
言語 Go
(1.22.1)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 505 bytes
コンパイル時間 15,262 ms
コンパイル使用メモリ 218,192 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 15:45:41
合計ジャッジ時間 16,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 276 ms
4,348 KB
testcase_07 AC 174 ms
4,352 KB
testcase_08 AC 172 ms
4,352 KB
testcase_09 AC 169 ms
4,348 KB
testcase_10 AC 171 ms
4,348 KB
testcase_11 AC 173 ms
4,348 KB
testcase_12 AC 173 ms
4,352 KB
testcase_13 AC 175 ms
4,352 KB
testcase_14 AC 175 ms
4,348 KB
testcase_15 AC 173 ms
4,352 KB
testcase_16 AC 173 ms
4,352 KB
testcase_17 AC 47 ms
4,348 KB
testcase_18 AC 48 ms
4,348 KB
testcase_19 AC 154 ms
4,352 KB
testcase_20 AC 44 ms
4,348 KB
testcase_21 AC 150 ms
4,348 KB
testcase_22 AC 104 ms
4,352 KB
testcase_23 AC 16 ms
4,348 KB
testcase_24 AC 6 ms
4,372 KB
testcase_25 AC 154 ms
4,348 KB
testcase_26 AC 69 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"bufio"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var N int
	fmt.Fscan(r, &N)
	var P []int = make([]int, N)
	var idx map[int]int = make(map[int]int)
	for i := 0; i < N; i++ {
		var x int
		fmt.Fscan(r, &x)
		P[i] = x
		idx[x] = i
	}
	cnt := 0
	for i := 0; i < N-1; i++ {
		for P[i] != i + 1 {
			j := idx[P[i]-1]
			idx[P[i]], idx[P[j]] = j, i
			P[i], P[j] = P[j], P[i]
			cnt++
		}
	}
	fmt.Fprintln(w, cnt)
}
0