結果

問題 No.738 平らな農地
ユーザー ikdikd
提出日時 2019-04-18 13:05:18
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 3,323 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 32,640 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-09-22 09:10:12
合計ジャッジ時間 12,367 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,944 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 228 ms
6,940 KB
testcase_16 AC 229 ms
6,944 KB
testcase_17 AC 234 ms
6,944 KB
testcase_18 AC 228 ms
6,940 KB
testcase_19 AC 165 ms
7,168 KB
testcase_20 AC 235 ms
6,944 KB
testcase_21 AC 250 ms
7,168 KB
testcase_22 AC 234 ms
6,944 KB
testcase_23 AC 232 ms
7,040 KB
testcase_24 AC 209 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 237 ms
7,296 KB
testcase_46 AC 223 ms
6,940 KB
testcase_47 AC 233 ms
7,040 KB
testcase_48 AC 211 ms
6,940 KB
testcase_49 AC 207 ms
6,940 KB
testcase_50 AC 218 ms
7,040 KB
testcase_51 AC 240 ms
7,168 KB
testcase_52 AC 223 ms
6,944 KB
testcase_53 AC 227 ms
7,040 KB
testcase_54 AC 245 ms
7,040 KB
testcase_55 AC 244 ms
7,168 KB
testcase_56 AC 240 ms
7,040 KB
testcase_57 AC 220 ms
6,944 KB
testcase_58 AC 230 ms
7,040 KB
testcase_59 AC 230 ms
7,296 KB
testcase_60 AC 221 ms
7,296 KB
testcase_61 AC 224 ms
7,168 KB
testcase_62 AC 213 ms
6,940 KB
testcase_63 AC 250 ms
7,296 KB
testcase_64 AC 234 ms
7,168 KB
testcase_65 AC 24 ms
6,944 KB
testcase_66 AC 24 ms
7,040 KB
testcase_67 AC 88 ms
6,940 KB
testcase_68 AC 90 ms
6,944 KB
testcase_69 AC 106 ms
6,944 KB
testcase_70 AC 100 ms
7,296 KB
testcase_71 AC 77 ms
7,168 KB
testcase_72 AC 90 ms
6,944 KB
testcase_73 AC 84 ms
6,940 KB
testcase_74 AC 79 ms
7,040 KB
testcase_75 AC 112 ms
6,940 KB
testcase_76 AC 117 ms
7,040 KB
testcase_77 AC 94 ms
6,944 KB
testcase_78 AC 102 ms
7,168 KB
testcase_79 AC 165 ms
7,296 KB
testcase_80 AC 164 ms
7,040 KB
testcase_81 AC 152 ms
6,944 KB
testcase_82 AC 142 ms
7,040 KB
testcase_83 AC 96 ms
7,296 KB
testcase_84 AC 174 ms
7,168 KB
testcase_85 AC 33 ms
7,296 KB
testcase_86 AC 35 ms
6,944 KB
testcase_87 AC 128 ms
7,040 KB
testcase_88 AC 123 ms
6,944 KB
testcase_89 AC 1 ms
6,944 KB
testcase_90 AC 1 ms
6,944 KB
testcase_91 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}

Node *merge(Node *left, Node *right) {
  if (left == NULL) {
    return right;
  } else if (right == NULL) {
    return left;
  }
  if (rand_int() % (size(left) + size(right)) < (unsigned int)size(left)) {
    left->right = merge(left->right, right);
    return update(left);
  } else {
    right->left = merge(left, right->left);
    return update(right);
  }
}

Pair split(Node *root, int k) {
  if (root == NULL) return make_pair(root, root);
  if (k <= size(root->left)) {
    Pair nodes = split(root->left, k);
    root->left = nodes.right;
    return make_pair(nodes.left, update(root));
  } else {
    Pair nodes = split(root->right, k - size(root->left) - 1);
    root->right = nodes.left;
    return make_pair(update(root), nodes.right);
  }
}

int count_less(Node *root, i64 x) {
  if (root == NULL) return 0;
  if (root->val < x) {
    return count_less(root->right, x) + size(root->left) + 1;
  } else {
    return count_less(root->left, x);
  }
}

Node *insert(Node *root, i64 x) {
  Pair nodes = split(root, count_less(root, x));
  return merge(merge(nodes.left, create_node(x)), nodes.right);
}

Node *erase(Node *root, i64 x) {
  Pair nodes = split(root, count_less(root, x));
  if (nodes.right != NULL) {
    return merge(nodes.left, split(nodes.right, 1).right);
  } else {
    return merge(nodes.left, nodes.right);
  }
}

Node *get_kth(Node *root, int k) {
  if (root == NULL || size(root) < k) return NULL;
  int left_size = size(root->left);
  if (left_size + 1 == k) {
    return root;
  } else if (left_size >= 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