Submission #5420212


Source Code Expand

#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 Info

Submission Time
Task E - Cookies
User sansen
Language C (GCC 5.4.1)
Score 500
Code Size 3247 Byte
Status TLE
Exec Time 2211 ms
Memory 1633792 KB

Compile Error

./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);
   ^

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 500 / 500 0 / 500
Status
AC × 3
AC × 27
AC × 35
TLE × 36
MLE × 1
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
dataset1 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 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
Case Name Status Exec Time Memory
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 2204 ms 1613696 KB
02-03.txt TLE 2205 ms 1624192 KB
02-04.txt TLE 2204 ms 1631488 KB
02-05.txt TLE 2204 ms 1632384 KB
02-06.txt TLE 2206 ms 1630080 KB
02-07.txt TLE 2204 ms 1627264 KB
02-08.txt TLE 2205 ms 1632896 KB
02-09.txt TLE 2204 ms 1630336 KB
02-10.txt TLE 2204 ms 1629568 KB
02-11.txt TLE 2207 ms 1631872 KB
02-12.txt TLE 2207 ms 1630720 KB
02-13.txt TLE 2204 ms 1632384 KB
02-14.txt TLE 2204 ms 1626624 KB
02-15.txt TLE 2211 ms 1614592 KB
02-16.txt TLE 2202 ms 1588864 KB
02-17.txt TLE 2197 ms 1513856 KB
02-18.txt TLE 2187 ms 1348992 KB
02-19.txt TLE 2178 ms 988544 KB
02-20.txt TLE 2134 ms 459904 KB
02-21.txt TLE 2110 ms 81920 KB
02-22.txt TLE 2104 ms 4480 KB
02-23.txt AC 12 ms 128 KB
02-24.txt AC 12 ms 128 KB
02-25.txt MLE 1290 ms 967040 KB
02-26.txt TLE 2211 ms 1629568 KB
02-27.txt TLE 2204 ms 1628672 KB
02-28.txt TLE 2205 ms 1631744 KB
02-29.txt TLE 2205 ms 1633408 KB
02-30.txt TLE 2204 ms 1632896 KB
02-31.txt TLE 2204 ms 1633408 KB
02-32.txt TLE 2204 ms 1633792 KB
02-33.txt TLE 2207 ms 1633536 KB
02-34.txt TLE 2204 ms 1633792 KB
02-35.txt TLE 2205 ms 1633408 KB
02-36.txt TLE 2203 ms 1619200 KB
02-37.txt TLE 2172 ms 1050240 KB
02-38.txt TLE 2125 ms 295680 KB
02-39.txt TLE 2104 ms 7424 KB
02-40.txt TLE 2103 ms 2944 KB
sample-01.txt AC 1 ms 128 KB
sample-02.txt AC 1 ms 128 KB
sample-03.txt AC 2 ms 1408 KB