結果

問題 No.2627 Unnatural Pitch
ユーザー 👑 chro_96chro_96
提出日時 2024-02-09 23:19:24
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,891 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 34,176 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-09 23:19:33
合計ジャッジ時間 3,492 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
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 AC 128 ms
6,676 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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 == n-idx || i+1 == n-idx || i == n-idx+1) {
      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 (n-i-1 == idx+1 || n-i == idx+1 || n-i-1 == idx+2) {
      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 >= 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));
        }
      }
      for (int j = 0; j < n; j++) {
        if (a[j] > a[i]) {
          b[j] = a[j]-(1LL+(a[i]-a[j]-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 < 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));
        }
      }
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0