CODE FESTIVAL 2016 Final

Submission #5420212

Source codeソースコード

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

typedef int32_t i32;
typedef int64_t i64;

typedef struct result {
  i64 n;
  i64 ans;
} result;

typedef result WBT_val;

static inline int compare_WBT_val (const WBT_val a, const WBT_val b) {
  i64 d = a.n - b.n;
  return d == 0 ? 0 : d < 0 ? -1 : 1;;
}

typedef struct weight_balanced_tree_node {
  struct weight_balanced_tree_node *left;
  struct weight_balanced_tree_node *right;
  i32 lsize;
  i32 rsize;
  WBT_val val;
} WBT_node;

WBT_node* new_WBT_node (const WBT_val val) {
  WBT_node *t = (WBT_node *) calloc (1, sizeof (WBT_node));
  t->left = NULL;
  t->right = NULL;
  t->lsize = 0;
  t->rsize = 0;
  t->val = val;
  return t;
}

i32 get_size (const WBT_node * const t) {
  return t == NULL ? 0 : t->lsize + t->rsize + 1;
}

int get_bias (const WBT_node * const t, const i32 b) {
  if (t == NULL) {
    return 0;
  }
  const i32 l = t->lsize + 1;
  const i32 r = t->rsize + 1;
  if (l * b >= r && l <= b * r) {
    return 0;
  }
  return l * b < r ? -1 : 1;
}

void WBT_node_update (WBT_node * const t) {
  if (t == NULL) return;
  t->lsize = get_size (t->left);
  t->rsize = get_size (t->right);
}

WBT_node* left_rotate (WBT_node * const u) {
  WBT_node * const v = u->right;
  u->right = v->left;
  v->left = u;
  WBT_node_update (u);
  WBT_node_update (v);
  return v;
}

WBT_node* right_rotate (WBT_node * const v) {
  WBT_node * const u = v->left;
  v->left = u->right;
  u->right = v;
  WBT_node_update (v);
  WBT_node_update (u);
  return u;
}

WBT_node* balance (WBT_node * const t) {
  i32 b = get_bias (t, 3);
  if (b == 0) {
    return t;
  }
  if (b < 0) {
    if (get_bias (t->right, 2) > 0) {
      t->right = right_rotate (t->right);
    }
    return left_rotate (t);
  } else {
    if (get_bias (t->left, 2) < 0) {
      t->left = left_rotate (t->left);
    }
    return right_rotate (t);
  }
}

WBT_node* insert (WBT_node * const t, const WBT_val val) {
  if (t == NULL) {
    return new_WBT_node (val);
  }
  int c = compare_WBT_val (val, t->val);
  if (c == 0) {
    t->val = val;
    return t;
  }
  if (c < 0) {
    t->left = insert (t->left, val);
  } else {
    t->right = insert (t->right, val);
  }
  WBT_node_update (t);
  return balance (t);
}

WBT_val search (WBT_node * const t, const WBT_val val) {
  if (t == NULL) return val;
  int c = compare_WBT_val (val, t->val);
  if (c == 0) return t->val;
  return c < 0 ? search (t->left, val) : search (t->right, val);
}

#define MIN(a,b) ((a) < (b) ? (a) : (b))

i64 calc (i64 n, i64 a, i64 begin) {
  if (n <= a) return n;
  static WBT_node *memo = NULL;
  result v = search (memo, (result) {n, n + 1});
  if (v.ans <= n) return v.ans;
  i64 min = n;
  for (i64 i = begin; i * i <= n; ++i) {
    i64 d = i;
    i64 x = (n + d - 1) / d;
    i64 t = d + a + calc (x, a, i);
    min = MIN(min, t);
  }
  insert (memo, (result) {n, min});
  return min;
}

void run (void) {
  i64 n, a;
  scanf ("%" SCNi64 "%" SCNi64, &n, &a);
  i64 ans = calc (n, a, 2);
  printf ("%" PRIi64 "\n", ans);
}

int main (void) {
  run();
  return 0;
}

Submission

Task問題 E - Cookies
User nameユーザ名 sansen
Created time投稿日時
Language言語 C (GCC 5.4.1)
Status状態 TLE
Score得点 500
Source lengthソースコード長 3247 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Compiler messageコンパイルメッセージ

./Main.c: In function ‘run’:
./Main.c:142:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf ("%" SCNi64 "%" SCNi64, &n, &a);
^

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt,sample-03.txt
dataset1 500 / 500 sample-01.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt
dataset2 0 / 500 sample-01.txt,sample-02.txt,sample-03.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,02-01.txt,02-02.txt,02-03.txt,02-04.txt,02-05.txt,02-06.txt,02-07.txt,02-08.txt,02-09.txt,02-10.txt,02-11.txt,02-12.txt,02-13.txt,02-14.txt,02-15.txt,02-16.txt,02-17.txt,02-18.txt,02-19.txt,02-20.txt,02-21.txt,02-22.txt,02-23.txt,02-24.txt,02-25.txt,02-26.txt,02-27.txt,02-28.txt,02-29.txt,02-30.txt,02-31.txt,02-32.txt,02-33.txt,02-34.txt,02-35.txt,02-36.txt,02-37.txt,02-38.txt,02-39.txt,02-40.txt,sample-01.txt,sample-02.txt,sample-03.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 13 ms 9216 KB
01-02.txt AC 13 ms 9216 KB
01-03.txt AC 13 ms 9216 KB
01-04.txt AC 13 ms 9216 KB
01-05.txt AC 13 ms 9216 KB
01-06.txt AC 13 ms 9216 KB
01-07.txt AC 13 ms 9216 KB
01-08.txt AC 12 ms 8704 KB
01-09.txt AC 10 ms 6528 KB
01-10.txt AC 5 ms 2560 KB
01-11.txt AC 5 ms 2560 KB
01-12.txt AC 1 ms 128 KB
01-13.txt AC 1 ms 128 KB
01-14.txt AC 1 ms 768 KB
01-15.txt AC 7 ms 4992 KB
01-16.txt AC 7 ms 4992 KB
01-17.txt AC 13 ms 9088 KB
01-18.txt AC 1 ms 128 KB
01-19.txt AC 1 ms 128 KB
01-20.txt AC 1 ms 128 KB
01-21.txt AC 1 ms 128 KB
01-22.txt AC 1 ms 128 KB
01-23.txt AC 1 ms 128 KB
01-24.txt AC 1 ms 128 KB
01-25.txt AC 1 ms 128 KB
01-26.txt AC 1 ms 128 KB
02-01.txt AC 1 ms 128 KB
02-02.txt TLE
02-03.txt TLE
02-04.txt TLE
02-05.txt TLE
02-06.txt TLE
02-07.txt TLE
02-08.txt TLE
02-09.txt TLE
02-10.txt TLE
02-11.txt TLE
02-12.txt TLE
02-13.txt TLE
02-14.txt TLE
02-15.txt TLE
02-16.txt TLE
02-17.txt TLE
02-18.txt TLE
02-19.txt TLE
02-20.txt TLE
02-21.txt TLE
02-22.txt TLE
02-23.txt AC 12 ms 128 KB
02-24.txt AC 12 ms 128 KB
02-25.txt MLE
02-26.txt TLE
02-27.txt TLE
02-28.txt TLE
02-29.txt TLE
02-30.txt TLE
02-31.txt TLE
02-32.txt TLE
02-33.txt TLE
02-34.txt TLE
02-35.txt TLE
02-36.txt TLE
02-37.txt TLE
02-38.txt TLE
02-39.txt TLE
02-40.txt TLE
sample-01.txt AC 1 ms 128 KB
sample-02.txt AC 1 ms 128 KB
sample-03.txt AC 2 ms 1408 KB