結果

問題 No.2665 Minimize Inversions of Deque
ユーザー 👑 chro_96chro_96
提出日時 2024-03-08 22:02:37
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 30,848 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 22:02:43
合計ジャッジ時間 5,229 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,548 KB
testcase_01 AC 49 ms
6,548 KB
testcase_02 AC 49 ms
6,676 KB
testcase_03 AC 48 ms
6,676 KB
testcase_04 AC 48 ms
6,676 KB
testcase_05 AC 48 ms
6,676 KB
testcase_06 AC 48 ms
6,676 KB
testcase_07 AC 48 ms
6,676 KB
testcase_08 AC 48 ms
6,676 KB
testcase_09 AC 48 ms
6,676 KB
testcase_10 AC 48 ms
6,676 KB
testcase_11 AC 47 ms
6,676 KB
testcase_12 AC 48 ms
6,676 KB
testcase_13 AC 48 ms
6,676 KB
testcase_14 AC 47 ms
6,676 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 47 ms
6,676 KB
testcase_17 AC 48 ms
6,676 KB
testcase_18 AC 48 ms
6,676 KB
testcase_19 AC 10 ms
6,676 KB
testcase_20 AC 5 ms
6,676 KB
testcase_21 AC 5 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 5 ms
6,676 KB
testcase_24 AC 6 ms
6,676 KB
testcase_25 AC 5 ms
6,676 KB
testcase_26 AC 5 ms
6,676 KB
testcase_27 AC 6 ms
6,676 KB
testcase_28 AC 6 ms
6,676 KB
testcase_29 AC 5 ms
6,676 KB
testcase_30 AC 94 ms
6,676 KB
testcase_31 AC 83 ms
6,676 KB
testcase_32 AC 84 ms
6,676 KB
testcase_33 AC 91 ms
6,676 KB
testcase_34 AC 82 ms
6,676 KB
testcase_35 AC 71 ms
6,676 KB
testcase_36 AC 72 ms
6,676 KB
testcase_37 AC 77 ms
6,676 KB
testcase_38 AC 80 ms
6,676 KB
testcase_39 AC 90 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void add_segt (int *t, int idx, int val, int size) {
  idx = idx+size-1;
  t[idx] += val;
  
  while (idx > 0) {
    idx = (idx-1)/2;
    t[idx] += val;
  }
  
  return;
}

int sum_segt_rec (int *t, int a, int b, int k, int l, int r) {
  int ans = 0;
  
  if (r <= a || b <= l) {
    return 0;
  }
  
  if (a <= l && r <= b) {
    return t[k];
  }
  
  ans = sum_segt_rec(t, a, b, 2*k+1, l, (l+r)/2);
  ans += sum_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);
  return ans;
}

int sum_segt (int *t, int a, int b, int size) {
  return sum_segt_rec(t, a, b, 0, 0, size);
}

int main () {
  int t = 0;
  int n = 0;
  int p = 0;
  
  int res = 0;
  
  int a[400000] = {};
  int st[524287] = {};
  
  res = scanf("%d", &t);
  while (t > 0) {
    long long x = 0LL;
    int hd = 0;
    int tl = 0;
    int size = 1;
    res = scanf("%d", &n);
    hd = n;
    tl = n;
    while (size <= n+1) {
      size <<= 1;
    }
    for (int i = 0; i < 2*size-1; i++) {
      st[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      res = scanf("%d", &p);
      cnt = sum_segt(st, 0, p, size);
      if (cnt < i-cnt || (cnt == i-cnt && a[hd] > p)) {
        hd--;
        a[hd] = p;
        x += (long long)cnt;
      } else {
        a[tl] = p;
        tl++;
        x += (long long)(i-cnt);
      }
      add_segt(st, p, 1, size);
    }
    printf("%lld\n", x);
    printf("%d", a[hd]);
    for (int i = hd+1; i < tl; i++) {
      printf(" %d", a[i]);
    }
    printf("\n");
    t--;
  }
  
  return 0;
}
0