結果

問題 No.2248 max(C)-min(C)
ユーザー 👑 chro_96chro_96
提出日時 2023-03-17 22:26:03
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,650 bytes
コンパイル時間 1,253 ms
コンパイル使用メモリ 30,840 KB
実行使用メモリ 8,216 KB
最終ジャッジ日時 2023-10-18 15:23:05
合計ジャッジ時間 5,500 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,216 KB
testcase_01 AC 4 ms
8,216 KB
testcase_02 AC 4 ms
8,216 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 49 ms
8,216 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void upheap (long long q[][2], int idx) {
  int pidx = (idx-1)/2;
  
  if (idx <= 0) {
    return;
  }
  
  if (q[pidx][0] < q[idx][0]) {
    long long swap = q[idx][0];
    q[idx][0] = q[pidx][0];
    q[pidx][0] = swap;
    swap = q[idx][1];
    q[idx][1] = q[pidx][1];
    q[pidx][1] = swap;
    upheap(q, pidx);
  }
  
  return;
}

void downheap (long long q[][2], int idx, int size) {
  long long ch[2] = { -1LL, -1LL };
  int chidx = -1;
  
  if (size <= 2*idx+1) {
    return;
  }
  
  ch[0] = q[2*idx+1][0];
  if (2*idx+2 < size) {
    ch[0] = q[2*idx+2][0];
  }
  
  if (ch[0] > ch[1] && ch[0] > q[idx][0]) {
    chidx = 2*idx+1;
  } else if (ch[1] > q[idx][0]) {
    chidx = 2*idx+2;
  }
  
  if (chidx > 0) {
    long long swap = q[idx][0];
    q[idx][0] = q[chidx][0];
    q[chidx][0] = swap;
    swap = q[idx][1];
    q[idx][1] = q[chidx][1];
    q[chidx][1] = swap;
    downheap(q, chidx, size);
  }
  
  return;
}

void dequeue (long long q[][2], int *size, long long *res) {
  res[0] = q[0][0];
  res[1] = q[0][1];
  if (*size > 0) {
    *size -= 1;
    if (*size > 0) {
      q[0][0] = q[*size][0];
      q[0][1] = q[*size][1];
      downheap(q, 0, *size);
    }
  }
  return;
}

int main () {
  int n = 0;
  long long a[200000] = {};
  long long b[200000] = {};
  
  int res = 0;
  
  long long ans = 0LL;
  long long max = 0LL;
  long long min = 2000000001LL;
  long long q[200000][2] = {};
  int qsize = 0;
  
  int is_fin = 0;
  
  res = scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
  }
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", b+i);
  }
  
  for (int i = 0; i < n; i++) {
    if (a[i] < b[i]) {
      long long swap = a[i];
      a[i] = b[i];
      b[i] = swap;
    }
    q[qsize][0] = a[i];
    q[qsize][1] = (long long)i;
    upheap(q, qsize);
    qsize++;
    if (a[i] > max) {
      max = a[i];
    }
    if (a[i] < min) {
      min = a[i];
    }
  }
  
  ans = max-min;
  while (is_fin <= 0) {
    long long res[2] = {};
    dequeue(q, &qsize, res);
    max = res[0];
    if (max-min < ans) {
      ans = max-min;
    }
    if (b[(int)res[1]] == res[0]) {
      is_fin = 1;
    } else if (a[(int)res[1]] == res[0]) {
      long long tmp = (res[0]+b[(int)res[1]])/2LL;
      if (tmp < min) {
        min = tmp;
      }
      q[qsize][0] = tmp;
      q[qsize][1] = res[1];
      upheap(q, qsize);
      qsize++;
    } else {
      long long tmp = b[(int)res[1]];
      if (tmp < min) {
        min = tmp;
      }
      q[qsize][0] = tmp;
      q[qsize][1] = res[1];
      upheap(q, qsize);
      qsize++;
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0