結果

問題 No.738 平らな農地
ユーザー hashiryohashiryo
提出日時 2020-08-27 15:25:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,899 bytes
コンパイル時間 1,834 ms
コンパイル使用メモリ 171,656 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-25 08:33:32
合計ジャッジ時間 6,969 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 38 ms
5,504 KB
testcase_16 AC 38 ms
5,632 KB
testcase_17 AC 43 ms
5,632 KB
testcase_18 AC 47 ms
5,504 KB
testcase_19 AC 49 ms
5,760 KB
testcase_20 AC 41 ms
5,760 KB
testcase_21 AC 47 ms
5,888 KB
testcase_22 AC 41 ms
5,632 KB
testcase_23 AC 46 ms
5,632 KB
testcase_24 AC 46 ms
5,632 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 42 ms
6,016 KB
testcase_46 AC 40 ms
5,760 KB
testcase_47 AC 43 ms
5,760 KB
testcase_48 AC 39 ms
5,632 KB
testcase_49 AC 37 ms
5,760 KB
testcase_50 AC 38 ms
5,760 KB
testcase_51 AC 43 ms
5,760 KB
testcase_52 AC 41 ms
5,632 KB
testcase_53 AC 41 ms
5,760 KB
testcase_54 AC 44 ms
5,888 KB
testcase_55 AC 41 ms
5,760 KB
testcase_56 AC 42 ms
5,760 KB
testcase_57 AC 38 ms
5,632 KB
testcase_58 AC 39 ms
5,760 KB
testcase_59 AC 41 ms
5,888 KB
testcase_60 AC 41 ms
6,016 KB
testcase_61 AC 40 ms
5,888 KB
testcase_62 AC 38 ms
5,632 KB
testcase_63 AC 43 ms
6,016 KB
testcase_64 AC 41 ms
5,888 KB
testcase_65 AC 49 ms
5,760 KB
testcase_66 AC 53 ms
5,632 KB
testcase_67 AC 29 ms
6,016 KB
testcase_68 AC 28 ms
6,144 KB
testcase_69 AC 35 ms
6,272 KB
testcase_70 AC 33 ms
6,272 KB
testcase_71 AC 10 ms
5,376 KB
testcase_72 AC 15 ms
5,376 KB
testcase_73 AC 14 ms
5,376 KB
testcase_74 AC 16 ms
5,376 KB
testcase_75 AC 42 ms
6,016 KB
testcase_76 AC 40 ms
6,144 KB
testcase_77 AC 43 ms
6,016 KB
testcase_78 AC 46 ms
6,400 KB
testcase_79 AC 41 ms
6,400 KB
testcase_80 AC 35 ms
6,272 KB
testcase_81 AC 36 ms
6,144 KB
testcase_82 AC 38 ms
6,272 KB
testcase_83 AC 14 ms
5,376 KB
testcase_84 AC 15 ms
5,376 KB
testcase_85 AC 62 ms
6,272 KB
testcase_86 AC 57 ms
6,016 KB
testcase_87 AC 34 ms
6,144 KB
testcase_88 AC 33 ms
6,016 KB
testcase_89 AC 2 ms
5,376 KB
testcase_90 AC 1 ms
5,376 KB
testcase_91 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                           \
  cerr << __LINE__ << ": " << #x << " = {";        \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
    cerr << ((hoge) ? "," : "") << x[hoge];        \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

#define TEST(s)                            \
  if (!(s)) {                              \
    cout << __LINE__ << " " << #s << endl; \
    exit(-1);                              \
  }
double tick() {
  static clock_t oldtick;
  clock_t newtick = clock();
  double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC;
  oldtick = newtick;
  return diff;
}

template <typename T>
struct BinaryIndexedTree {
  vector<T> dat;
  BinaryIndexedTree(int n) : dat(n + 1, 0) {}
  BinaryIndexedTree(int n, T a) : BinaryIndexedTree(vector<T>(n, a)) {}
  BinaryIndexedTree(vector<T> y) : dat(y.size() + 1) {
    for (int i = 0; i < y.size(); ++i) dat[i + 1] = y[i];
    for (int i = 1; i + (i & -i) < dat.size(); ++i) dat[i + (i & -i)] += dat[i];
  }
  void add(int i, T a = 1) {
    for (++i; i < (int)dat.size(); i += i & -i) dat[i] += a;
  }
  T sum(int i) {  // sum [0,i)
    T s = 0;
    for (; i > 0; i &= i - 1) s += dat[i];
    return s;
  }
  // min { i : sum(i+1) > k } -> kth element(0-indexed)
  int find(T k) const {
    int i = 0;
    for (int p = 1 << (__lg(dat.size() - 1) + 1); p > 0; p >>= 1)
      if (i + p < (int)dat.size() && dat[i + p] <= k) k -= dat[i += p];
    return i + 1 == (int)dat.size() ? -1 : i;  // -1 -> no solutions
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, K;
  cin >> N >> K;
  long long A[N];
  for (int i = 0; i < N; i++) cin >> A[i];
  vector<long long> vec(A, A + N);
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  auto id = [&](int x) {
    return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
  };
  BinaryIndexedTree<long long> bit1(vec.size()), bit2(vec.size());
  long long ans = LLONG_MAX / 10;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    int idx = id(A[i]);
    bit1.add(idx, 1);
    bit2.add(idx, A[i]);
    sum += A[i];
    if (i >= K - 1) {
      int med = bit1.find(K / 2);
      int lcnt = bit1.sum(med), hcnt = K - lcnt;
      long long lsum = bit2.sum(med), hsum = sum - lsum;
      long long low = lcnt * vec[med] - lsum;
      long long high = hsum - hcnt * vec[med];
      ans = min(ans, low + high);
      int jdx = id(A[i - K + 1]);
      bit1.add(jdx, -1);
      bit2.add(jdx, -A[i - K + 1]);
      sum -= A[i - K + 1];
    }
  }
  cout << ans << "\n";
  return 0;
}
0