結果

問題 No.1604 Swap Sort:ONE
ユーザー bonKotsu
提出日時 2021-07-21 00:50:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 482 bytes
コンパイル時間 1,701 ms
コンパイル使用メモリ 169,264 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-17 13:52:50
合計ジャッジ時間 2,682 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>

int main() {
  int n;
  cin >> n;
  vector<int> a(n),pos(n);
  rep(i,n) {
    cin >> a[i];
    a[i]--;
    pos[a[i]] = i;
  }

  int ans = 0;
  rep(i,n) {
    if (a[i] == i) continue;
    int now = a[i];
    rep(j,now-i) {
      ans++;
      swap(a[i],a[pos[a[i]-1]]);
      swap(pos[a[i]],pos[a[i]+1]);
    }
  }
  cout << ans << endl;


}
0