結果

問題 No.1604 Swap Sort:ONE
ユーザー merlinmerlin
提出日時 2021-07-16 21:46:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 75,668 KB
実行使用メモリ 56,628 KB
最終ジャッジ日時 2023-09-20 13:32:15
合計ジャッジ時間 7,231 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,156 KB
testcase_01 AC 45 ms
49,376 KB
testcase_02 AC 42 ms
49,244 KB
testcase_03 AC 42 ms
49,472 KB
testcase_04 AC 42 ms
49,500 KB
testcase_05 AC 59 ms
51,552 KB
testcase_06 AC 193 ms
55,492 KB
testcase_07 AC 159 ms
55,596 KB
testcase_08 AC 170 ms
56,148 KB
testcase_09 AC 191 ms
56,064 KB
testcase_10 AC 196 ms
56,608 KB
testcase_11 AC 162 ms
55,232 KB
testcase_12 AC 162 ms
56,084 KB
testcase_13 AC 169 ms
56,236 KB
testcase_14 AC 215 ms
56,336 KB
testcase_15 AC 165 ms
56,036 KB
testcase_16 AC 198 ms
56,404 KB
testcase_17 AC 143 ms
55,248 KB
testcase_18 AC 144 ms
55,844 KB
testcase_19 AC 161 ms
55,896 KB
testcase_20 AC 158 ms
55,720 KB
testcase_21 AC 191 ms
56,628 KB
testcase_22 AC 166 ms
56,264 KB
testcase_23 AC 113 ms
55,864 KB
testcase_24 AC 72 ms
53,948 KB
testcase_25 AC 190 ms
56,072 KB
testcase_26 AC 203 ms
56,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int n=Integer.parseInt(bu.readLine());
        String s[]=bu.readLine().split(" ");
        int i,a[]=new int[n],c[]=new int[n];
        for(i=0;i<n;i++)
        {
            a[i]=Integer.parseInt(s[i])-1;
            c[a[i]]=i;
        }

        int ans=0;
        for(i=0;i<n;i++)
        if(a[i]!=i)
        {
            ArrayList<Integer> el=new ArrayList<>();
            int x=i;
            while(a[x]!=i)
            {
                //System.out.print(x+" ");
                el.add(x);
                x=c[a[x]-1];
            }
            el.add(x);
            //System.out.println(i+" "+el);
            for(x=el.size()-2;x>=0;x--)
            {
                int u=el.get(x),v=el.get(x+1);
                a[u]=a[u]^a[v]^(a[v]=a[u]);
                c[a[u]]=u;
                c[a[v]]=v;
                ans++;
            }
        }
        System.out.print(ans);
    }
}
0