結果

問題 No.2248 max(C)-min(C)
ユーザー 👑 chro_96chro_96
提出日時 2023-03-17 22:28:14
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 154 ms / 3,000 ms
コード長 2,650 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 30,504 KB
実行使用メモリ 8,212 KB
最終ジャッジ日時 2023-10-18 15:24:41
合計ジャッジ時間 4,611 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,212 KB
testcase_01 AC 3 ms
8,212 KB
testcase_02 AC 3 ms
8,212 KB
testcase_03 AC 4 ms
8,212 KB
testcase_04 AC 3 ms
8,212 KB
testcase_05 AC 3 ms
8,212 KB
testcase_06 AC 4 ms
8,212 KB
testcase_07 AC 4 ms
8,212 KB
testcase_08 AC 4 ms
8,212 KB
testcase_09 AC 4 ms
8,212 KB
testcase_10 AC 3 ms
8,212 KB
testcase_11 AC 4 ms
8,212 KB
testcase_12 AC 4 ms
8,212 KB
testcase_13 AC 4 ms
8,212 KB
testcase_14 AC 4 ms
8,212 KB
testcase_15 AC 4 ms
8,212 KB
testcase_16 AC 4 ms
8,212 KB
testcase_17 AC 4 ms
8,212 KB
testcase_18 AC 54 ms
8,212 KB
testcase_19 AC 10 ms
8,212 KB
testcase_20 AC 60 ms
8,212 KB
testcase_21 AC 58 ms
8,212 KB
testcase_22 AC 41 ms
8,212 KB
testcase_23 AC 28 ms
8,212 KB
testcase_24 AC 13 ms
8,212 KB
testcase_25 AC 56 ms
8,212 KB
testcase_26 AC 50 ms
8,212 KB
testcase_27 AC 55 ms
8,212 KB
testcase_28 AC 8 ms
8,212 KB
testcase_29 AC 52 ms
8,212 KB
testcase_30 AC 23 ms
8,212 KB
testcase_31 AC 61 ms
8,212 KB
testcase_32 AC 15 ms
8,212 KB
testcase_33 AC 22 ms
8,212 KB
testcase_34 AC 61 ms
8,212 KB
testcase_35 AC 60 ms
8,212 KB
testcase_36 AC 61 ms
8,212 KB
testcase_37 AC 34 ms
8,212 KB
testcase_38 AC 56 ms
8,212 KB
testcase_39 AC 56 ms
8,212 KB
testcase_40 AC 56 ms
8,212 KB
testcase_41 AC 48 ms
8,212 KB
testcase_42 AC 56 ms
8,212 KB
testcase_43 AC 50 ms
8,212 KB
testcase_44 AC 56 ms
8,212 KB
testcase_45 AC 44 ms
8,212 KB
testcase_46 AC 25 ms
8,212 KB
testcase_47 AC 56 ms
8,212 KB
testcase_48 AC 88 ms
8,212 KB
testcase_49 AC 154 ms
8,212 KB
testcase_50 AC 154 ms
8,212 KB
testcase_51 AC 151 ms
8,212 KB
testcase_52 AC 152 ms
8,212 KB
testcase_53 AC 23 ms
8,212 KB
権限があれば一括ダウンロードができます

ソースコード

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[1] = 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