結果

問題 No.1604 Swap Sort:ONE
ユーザー ks2m
提出日時 2021-07-16 21:34:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 596 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 74,636 KB
実行使用メモリ 45,348 KB
最終ジャッジ日時 2024-07-06 08:31:05
合計ジャッジ時間 7,133 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] p = new int[n];
		for (int i = 0; i < n; i++) {
			p[i] = sc.nextInt() - 1;
		}
		sc.close();

		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[p[i]] = i;
		}

		int ans = 0;
		for (int i = 0; i < n; i++) {
			if (p[i] == i) {
				continue;
			}
			while (p[i] > i) {
				int x = a[p[i] - 1];
				a[p[i] - 1] = i;
				a[p[i]] = x;
				p[i]--;
				p[x]++;
				ans++;
			}
		}
		System.out.println(ans);
	}
}
0