結果

問題 No.1604 Swap Sort:ONE
ユーザー ks2mks2m
提出日時 2021-07-16 21:34:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 596 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 72,872 KB
実行使用メモリ 59,940 KB
最終ジャッジ日時 2023-09-20 13:13:51
合計ジャッジ時間 9,231 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
55,756 KB
testcase_01 AC 131 ms
55,864 KB
testcase_02 AC 130 ms
55,896 KB
testcase_03 AC 131 ms
56,056 KB
testcase_04 AC 130 ms
55,852 KB
testcase_05 AC 216 ms
58,332 KB
testcase_06 AC 288 ms
59,812 KB
testcase_07 AC 257 ms
59,424 KB
testcase_08 AC 251 ms
59,360 KB
testcase_09 AC 245 ms
59,728 KB
testcase_10 AC 247 ms
59,288 KB
testcase_11 AC 246 ms
59,420 KB
testcase_12 AC 248 ms
59,180 KB
testcase_13 AC 248 ms
59,188 KB
testcase_14 AC 247 ms
59,284 KB
testcase_15 AC 245 ms
59,748 KB
testcase_16 AC 244 ms
59,636 KB
testcase_17 AC 219 ms
58,800 KB
testcase_18 AC 215 ms
58,652 KB
testcase_19 AC 244 ms
59,412 KB
testcase_20 AC 215 ms
58,472 KB
testcase_21 AC 240 ms
59,556 KB
testcase_22 AC 227 ms
59,144 KB
testcase_23 AC 202 ms
56,712 KB
testcase_24 AC 197 ms
57,264 KB
testcase_25 AC 244 ms
59,940 KB
testcase_26 AC 226 ms
59,268 KB
権限があれば一括ダウンロードができます

ソースコード

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