結果

問題 No.2627 Unnatural Pitch
ユーザー 👑 chro_96chro_96
提出日時 2024-02-10 00:29:11
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 4,025 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 34,620 KB
実行使用メモリ 6,560 KB
最終ジャッジ日時 2024-02-10 00:29:17
合計ジャッジ時間 4,413 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,548 KB
testcase_01 AC 3 ms
6,548 KB
testcase_02 AC 3 ms
6,548 KB
testcase_03 AC 3 ms
6,548 KB
testcase_04 AC 122 ms
6,548 KB
testcase_05 AC 119 ms
6,548 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 149 ms
6,548 KB
testcase_09 AC 121 ms
6,560 KB
testcase_10 AC 124 ms
6,560 KB
testcase_11 AC 5 ms
6,548 KB
testcase_12 AC 4 ms
6,548 KB
testcase_13 AC 3 ms
6,548 KB
testcase_14 AC 5 ms
6,548 KB
testcase_15 AC 4 ms
6,548 KB
testcase_16 AC 219 ms
6,548 KB
testcase_17 AC 253 ms
6,548 KB
testcase_18 AC 361 ms
6,548 KB
testcase_19 AC 206 ms
6,548 KB
testcase_20 AC 115 ms
6,548 KB
testcase_21 AC 134 ms
6,548 KB
testcase_22 AC 237 ms
6,548 KB
testcase_23 AC 105 ms
6,548 KB
testcase_24 AC 143 ms
6,548 KB
testcase_25 AC 146 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int cmp_ll (const void *ap, const void *bp) {
  long long a = *(long long *)ap;
  long long b = *(long long *)bp;
  
  if (a > b) {
    return 1;
  }
  
  if (a < b) {
    return -1;
  }
  
  return 0;
}

int main () {
  int n = 0;
  long long k = 0LL;
  long long l = 0LL;
  long long u = 0LL;
  long long a[200000] = {};
  
  int res = 0;
  
  long long ans = 1000000000000000000LL;
  int idx = 0;
  
  long long b[200000] = {};
  
  res = scanf("%d", &n);
  res = scanf("%lld", &k);
  res = scanf("%lld", &l);
  res = scanf("%lld", &u);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", a+i);
  }
  
  qsort(a, n, sizeof(long long), cmp_ll);
  
  for (int i = 0; i < n; i++) {
    while (idx < n && a[i]+u-l >= a[idx]) {
      idx++;
    }
    if ((i == 0 || a[i] != a[i-1]) && (i == n-idx || i+1 == n-idx || i == n-idx+1 || i+2 == n-idx || i == n-idx+2)) {
      long long tmp = 0LL;
      int tidx = 0;
      for (int j = 0; j < n; j++) {
        if (a[j] < a[i]) {
          tmp += 1LL+(a[i]-a[j]-1LL)/k;
        } else if (a[j] > a[i]+u-l) {
          tmp += 1LL+(a[j]-a[i]-u+l-1LL)/k;
        }
      }
      if (tmp < ans) {
        ans = tmp;
      }
      for (int j = 0; j < n; j++) {
        if (a[j] < a[i]) {
          b[j] = ((a[i]-a[j]-1LL)/k)*k+a[j];
        } else if (a[j] > a[i]+u-l) {
          b[j] = a[j]-(1LL+(a[j]-a[i]-u+l-1LL)/k)*k;
        } else {
          b[j] = a[j];
        }
      }
      qsort(b, n, sizeof(long long), cmp_ll);
      for (int j = 0; j < i; j++) {
        while (tidx < n && b[j]+u-l >= b[tidx]) {
          tidx++;
        }
        if (tmp-((long long)(i-j-n-tidx)) < ans) {
          ans = tmp-((long long)(i-j-n-tidx));
        }
      }
      for (int j = 0; j < n; j++) {
        if (a[j] < a[i]) {
          b[j] = (1LL+(a[i]-a[j]-1LL)/k)*k+a[j];
        } else if (a[j] > a[i]+u-l) {
          b[j] = a[j]-((a[j]-a[i]-u+l-1LL)/k)*k;
        } else {
          b[j] = a[j];
        }
      }
      qsort(b, n, sizeof(long long), cmp_ll);
      tidx = n-1;
      for (int j = n-1; j >= idx; j--) {
        while (tidx >= 0 && b[j]-u+l <= b[tidx]) {
          tidx--;
        }
        if (tmp-((long long)(j+1-idx-tidx-1)) < ans) {
          ans = tmp-((long long)(j+1-idx-tidx-1));
        }
      }
    }
  }
  
  idx = n-1;
  for (int i = n-1; i >= 0; i--) {
    while (idx >= 0 && a[i]-u+l <= a[idx]) {
      idx--;
    }
    if ((i == n-1 || a[i] != a[i+1]) && (n-i-1 == idx+1 || n-i == idx+1 || n-i-1 == idx+2 || n-i-1 == idx+3 || n-i+1 == idx+1)) {
      long long tmp = 0LL;
      int tidx = 0;
      for (int j = 0; j < n; j++) {
        if (a[j] > a[i]) {
          tmp += 1LL+(a[j]-a[i]-1LL)/k;
        } else if (a[j] < a[i]-u+l) {
          tmp += 1LL+(a[i]-u+l-a[j]-1LL)/k;
        }
      }
      if (tmp < ans) {
        ans = tmp;
      }
      for (int j = 0; j < n; j++) {
        if (a[j] > a[i]) {
          b[j] = a[j]-((a[j]-a[i]-1LL)/k)*k;
        } else if (a[j] < a[i]-u+l) {
          b[j] = a[j]+(1LL+(a[i]-a[j]-u+l-1LL)/k)*k;
        } else {
          b[j] = a[j];
        }
      }
      qsort(b, n, sizeof(long long), cmp_ll);
      tidx = n-1;
      for (int j = n-1; j > i; j--) {
        while (tidx >= 0 && b[j]-u+l <= b[tidx]) {
          tidx--;
        }
        if (tmp-((long long)(j-i-tidx-1)) < ans) {
          ans = tmp-((long long)(j-i-tidx-1));
        }
      }
      for (int j = 0; j < n; j++) {
        if (a[j] > a[i]) {
          b[j] = a[j]-(1LL+(a[j]-a[i]-1LL)/k)*k;
        } else if (a[j] < a[i]-u+l) {
          b[j] = a[j]+((a[i]-a[j]-u+l-1LL)/k)*k;
        } else {
          b[j] = a[j];
        }
      }
      qsort(b, n, sizeof(long long), cmp_ll);
      tidx = 0;
      for (int j = 0; j <= idx; j++) {
        while (tidx < n && b[j]+u-l >= b[tidx]) {
          tidx++;
        }
        if (tmp-((long long)(idx-j+1-n+tidx)) < ans) {
          ans = tmp-((long long)(idx-j+1-n+tidx));
        }
      }
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0