結果

問題 No.738 平らな農地
ユーザー ikdikd
提出日時 2019-04-18 09:21:09
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 4,205 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 32,604 KB
実行使用メモリ 7,648 KB
最終ジャッジ日時 2023-10-22 07:41:56
合計ジャッジ時間 19,283 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 7 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 368 ms
6,896 KB
testcase_16 AC 380 ms
6,972 KB
testcase_17 AC 367 ms
6,940 KB
testcase_18 AC 362 ms
6,840 KB
testcase_19 AC 237 ms
7,260 KB
testcase_20 AC 373 ms
6,976 KB
testcase_21 AC 387 ms
7,284 KB
testcase_22 AC 383 ms
7,008 KB
testcase_23 AC 362 ms
7,116 KB
testcase_24 AC 320 ms
7,016 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 369 ms
7,364 KB
testcase_46 AC 353 ms
6,888 KB
testcase_47 AC 360 ms
7,192 KB
testcase_48 AC 330 ms
6,996 KB
testcase_49 AC 324 ms
6,948 KB
testcase_50 AC 333 ms
7,172 KB
testcase_51 AC 375 ms
7,228 KB
testcase_52 AC 349 ms
6,952 KB
testcase_53 AC 362 ms
7,052 KB
testcase_54 AC 367 ms
7,240 KB
testcase_55 AC 377 ms
7,228 KB
testcase_56 AC 364 ms
7,144 KB
testcase_57 AC 330 ms
6,848 KB
testcase_58 AC 347 ms
7,116 KB
testcase_59 AC 361 ms
7,372 KB
testcase_60 AC 349 ms
7,312 KB
testcase_61 AC 353 ms
7,272 KB
testcase_62 AC 333 ms
6,888 KB
testcase_63 AC 387 ms
7,364 KB
testcase_64 AC 368 ms
7,268 KB
testcase_65 AC 24 ms
6,960 KB
testcase_66 AC 24 ms
7,168 KB
testcase_67 AC 115 ms
6,892 KB
testcase_68 AC 120 ms
7,052 KB
testcase_69 TLE -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

unsigned int rand_int(void) {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

typedef long long i64;

typedef struct Node {
  i64 val;
  i64 sum;
  int size;
  struct Node *left;
  struct Node *right;
} Node;
Node *create_node(i64 val) {
  Node *ret = (Node *)malloc(sizeof(Node));
  ret->val = val;
  ret->sum = val;
  ret->size = 1;
  ret->left = ret->right = NULL;
  return ret;
}

typedef struct Pair {
  Node *left, *right;
} Pair;
Pair make_pair(Node *left, Node *right) {
  Pair pair = {left, right};
  return pair;
}

int size(Node *node) { return node == NULL ? 0 : node->size; }
i64 sum(Node *node) { return node == NULL ? 0 : node->sum; }

Node *update(Node *node) {
  node->size = 1 + size(node->left) + size(node->right);
  node->sum = node->val + sum(node->left) + sum(node->right);
  return node;
}

// left内の最大 <= right内の最小
Node *merge(Node *left, Node *right) {
  if (left == NULL) {
    return right;
  } else if (right == NULL) {
    return left;
  }
  // 大きい部分木が根に選ばれやすくする
  // 各node等確率で選ばれてほしいため
  if ((int)rand_int() % (size(left) + size(right)) < size(left)) {
    left->right = merge(left->right, right);
    return update(left);
  } else {
    right->left = merge(left, right->left);
    return update(right);
  }
}

// 小さいのk個を左側に、それ以外を右側に
Pair split(Node *root, int k) {
  if (root == NULL) return make_pair(root, root);
  if (k <= size(root->left)) { // root+右部分木はぜんぶ右側
    Pair nodes = split(root->left, k);
    root->left = nodes.right; // 左側からはみ出した分を拾う
    return make_pair(nodes.left, update(root));
  } else { // root+左部分木はぜんぶ左側
           // 右部分木から足りない分集める
    Pair nodes = split(root->right, k - size(root->left) - 1);
    root->right = nodes.left; // 補充
    return make_pair(update(root), nodes.right);
  }
}

// xより小さい数の個数
int lower_bound(Node *root, i64 x) {
  if (root == NULL) return 0;
  if (x <= root->val) {
    // 右部分木には1個もない
    return lower_bound(root->left, x);
  } else {
    // root+左部分木ぜんぶ は確定
    return lower_bound(root->right, x) + size(root->left) + 1;
  }
}

// ただの二分探索?
int exist(Node *root, i64 x) {
  if (root == NULL) return 0;
  if (x == root->val) {
    return 1;
  } else if (x < root->val) {
    return exist(root->left, x);
  } else {
    return exist(root->right, x);
  }
}

Node *insert(Node *root, i64 x) {
  Pair nodes = split(root, lower_bound(root, x));
  // merge(left, merge(new, right)) でもいい
  return merge(merge(nodes.left, create_node(x)), nodes.right);
}

Node *erase(Node *root, i64 x) {
  if (!exist(root, x)) return root;
  Pair nodes = split(root, lower_bound(root, x));
  // xはnodes.rightに含まれる(しかもroot?)
  return merge(nodes.left, split(nodes.right, 1).right);
}

// k番目に小さいnode
Node *get_kth(Node *root, int k) {
  if (root == NULL) return NULL;
  int left_size = size(root->left);
  if (left_size + 1 == k) {
    return root; // 右の部分木にroot->valと等しいのはあるかもしれない
  } else if (left_size + 1 > k) {
    return get_kth(root->left, k);
  } else {
    return get_kth(root->right, k - left_size - 1);
  }
}

int main(void) {

  Node *root = NULL;
  int n, k;
  scanf("%d %d", &n, &k);
  i64 *a = (i64 *)malloc(n * sizeof(i64));
  i64 ans = 1000000000000000000;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    root = insert(root, a[i]);
    if (i >= k) root = erase(root, a[i - k]);
    if (i + 1 >= k) {
      Node *node = get_kth(root, (k + 1) / 2);
      Pair pair = split(root, (k + 1) / 2);
      i64 cost = 0;
      cost += node->val * size(pair.left) - sum(pair.left);
      cost += sum(pair.right) - node->val * size(pair.right);
      if (cost < ans) ans = cost;
      root = merge(pair.left, pair.right);
    }
  }
  printf("%lld\n", ans);

  return 0;
}
0