結果

問題 No.1604 Swap Sort:ONE
ユーザー nebukuro09nebukuro09
提出日時 2021-07-16 21:41:32
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 681 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 131,608 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 11:44:38
合計ジャッジ時間 2,218 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib, std.datetime;


void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    foreach (i; 0..N) A[i]--;
    auto B = new int[](N);
    foreach (i, a; A) B[a] = i.to!int;

    long search (int i) {
        if (A[i] == i) return 0;
        int j = B[A[i]-1];
        swap(B[A[i]], B[A[i]-1]);
        swap(A[i], A[j]);
        return search(i) + 1;
    }

    long ans = 0;
    foreach (i; 0..N) {
        ans += search(i);
    }

    writeln(ans);
}
0