結果

問題 No.2029 Swap Min Max Min
ユーザー Iván SotoIván Soto
提出日時 2022-08-06 00:33:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,175 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 202,808 KB
実行使用メモリ 6,132 KB
最終ジャッジ日時 2023-10-14 02:51:19
合計ジャッジ時間 5,023 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 13 ms
4,720 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 7 ms
4,352 KB
testcase_06 AC 14 ms
4,352 KB
testcase_07 AC 14 ms
4,980 KB
testcase_08 AC 19 ms
5,472 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 10 ms
4,348 KB
testcase_13 AC 20 ms
5,660 KB
testcase_14 AC 20 ms
4,660 KB
testcase_15 AC 21 ms
6,088 KB
testcase_16 AC 20 ms
4,480 KB
testcase_17 AC 20 ms
5,576 KB
testcase_18 AC 22 ms
5,852 KB
testcase_19 AC 21 ms
4,436 KB
testcase_20 AC 20 ms
4,472 KB
testcase_21 AC 20 ms
4,436 KB
testcase_22 AC 20 ms
4,448 KB
testcase_23 AC 21 ms
6,004 KB
testcase_24 AC 21 ms
6,132 KB
testcase_25 AC 19 ms
4,468 KB
testcase_26 AC 19 ms
4,476 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 18 ms
5,556 KB
testcase_39 AC 19 ms
4,476 KB
testcase_40 AC 17 ms
4,360 KB
testcase_41 AC 20 ms
5,540 KB
testcase_42 AC 20 ms
5,536 KB
testcase_43 AC 19 ms
4,624 KB
testcase_44 AC 21 ms
5,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <algo/debug.h>
#else
#define debug(...) 42
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] <= n / 2) {
      pos.push_back(i);
    }
  }
  long long ans = 0;
  if (n % 2 == 1) {
    int p = 1;
    for (int i = 0; i < (int) pos.size(); i++) {
      ans += abs(pos[i] - p);
      p += 2;
    }
  } else {
    ans = (long long) 1e18;
    vector<long long> l((int) pos.size()), r((int) pos.size());
    {
      int p = 1;
      for (int i = 0; i < (int) pos.size(); i++) {
        l[i] = (i - 1 < 0 ? 0 : l[i - 1]) + abs(pos[i] - p);
        p += 2;
      }
    }
    {
      int p = n - 2;
      for (int i = (int) pos.size() - 1; i >= 0; i--) {
        r[i] = (i + 1 < (int) pos.size() ? r[i + 1] : 0) + abs(pos[i] - p);
        p -= 2;
      }
    }
    for (int i = 0; i < (int) pos.size() - 1; i++) {
      ans = min(ans, l[i] + r[i + 1]);
    }
    ans = min(ans, l.back());
    ans = min(ans, r[0]);
  }
  cout << n / 2 << ' ' << ans << '\n';
  return 0;
}
0