結果

問題 No.1604 Swap Sort:ONE
ユーザー tentententen
提出日時 2021-07-20 09:33:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,246 bytes
コンパイル時間 2,035 ms
コンパイル使用メモリ 74,852 KB
実行使用メモリ 52,328 KB
最終ジャッジ日時 2023-09-24 12:46:26
合計ジャッジ時間 5,607 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,296 KB
testcase_01 AC 44 ms
49,404 KB
testcase_02 AC 44 ms
49,408 KB
testcase_03 AC 44 ms
49,344 KB
testcase_04 AC 44 ms
49,328 KB
testcase_05 AC 77 ms
50,408 KB
testcase_06 AC 81 ms
51,840 KB
testcase_07 AC 79 ms
51,728 KB
testcase_08 AC 79 ms
50,136 KB
testcase_09 AC 80 ms
51,940 KB
testcase_10 AC 80 ms
51,764 KB
testcase_11 AC 79 ms
51,728 KB
testcase_12 AC 80 ms
51,856 KB
testcase_13 AC 79 ms
51,964 KB
testcase_14 AC 79 ms
50,004 KB
testcase_15 AC 79 ms
51,864 KB
testcase_16 AC 81 ms
51,636 KB
testcase_17 AC 88 ms
52,272 KB
testcase_18 AC 76 ms
51,544 KB
testcase_19 AC 81 ms
52,192 KB
testcase_20 AC 87 ms
52,328 KB
testcase_21 AC 80 ms
51,544 KB
testcase_22 AC 77 ms
51,588 KB
testcase_23 AC 57 ms
50,504 KB
testcase_24 AC 51 ms
49,432 KB
testcase_25 AC 79 ms
52,252 KB
testcase_26 AC 82 ms
51,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] positions = new int[n];
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt() - 1;
            positions[arr[i]] = i;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = arr[i]; j > i; j--) {
                arr[positions[j - 1]] = j;
                positions[j] = positions[j - 1];
                ans++;
            }
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0