結果

問題 No.1604 Swap Sort:ONE
ユーザー tentententen
提出日時 2023-02-21 13:08:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 2,039 bytes
コンパイル時間 6,485 ms
コンパイル使用メモリ 86,696 KB
実行使用メモリ 54,060 KB
最終ジャッジ日時 2023-09-29 06:26:03
合計ジャッジ時間 6,843 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,400 KB
testcase_01 AC 45 ms
49,404 KB
testcase_02 AC 44 ms
49,476 KB
testcase_03 AC 43 ms
49,540 KB
testcase_04 AC 45 ms
50,056 KB
testcase_05 AC 63 ms
51,756 KB
testcase_06 AC 90 ms
51,688 KB
testcase_07 AC 85 ms
51,764 KB
testcase_08 AC 82 ms
52,048 KB
testcase_09 AC 84 ms
51,976 KB
testcase_10 AC 84 ms
51,536 KB
testcase_11 AC 81 ms
52,012 KB
testcase_12 AC 82 ms
51,560 KB
testcase_13 AC 82 ms
52,272 KB
testcase_14 AC 82 ms
51,896 KB
testcase_15 AC 82 ms
51,768 KB
testcase_16 AC 90 ms
51,784 KB
testcase_17 AC 80 ms
52,884 KB
testcase_18 AC 75 ms
51,584 KB
testcase_19 AC 82 ms
51,440 KB
testcase_20 AC 78 ms
51,732 KB
testcase_21 AC 83 ms
51,792 KB
testcase_22 AC 80 ms
51,652 KB
testcase_23 AC 60 ms
49,604 KB
testcase_24 AC 52 ms
49,556 KB
testcase_25 AC 83 ms
54,060 KB
testcase_26 AC 81 ms
51,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        int[] places = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt() - 1;
            places[values[i]] = i;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            while (places[i] > i) {
                int tmp = values[i];
                int idx = places[tmp - 1];
                values[idx] = tmp;
                values[i] = tmp - 1;
                places[tmp] = idx;
                places[tmp - 1] = i; 
                ans++;
            }
        }
        System.out.println(ans);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0