結果

問題 No.1604 Swap Sort:ONE
コンテスト
ユーザー tenten
提出日時 2024-04-24 13:04:29
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,702 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,255 ms
コンパイル使用メモリ 83,564 KB
実行使用メモリ 43,236 KB
最終ジャッジ日時 2026-05-07 03:07:51
合計ジャッジ時間 6,339 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.*;
import java.util.*;
import java.util.function.*;
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[] positions = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt() - 1;
            positions[values[i]] = i;
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            while (values[i] > i) {
                int p = positions[values[i] - 1];
                values[p] = values[i];
                values[i] = values[i] - 1;
                positions[values[p]] = p;
                positions[values[i]] = i;
                count++;
            }
        }
        System.out.println(count);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0