結果

問題 No.777 再帰的ケーキ
ユーザー akakimidoriakakimidori
提出日時 2019-05-19 05:00:24
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,781 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 31,360 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2023-10-17 07:51:51
合計ジャッジ時間 3,394 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 0 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 0 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 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 172 ms
7,040 KB
testcase_29 AC 172 ms
7,040 KB
testcase_30 AC 180 ms
7,040 KB
testcase_31 AC 182 ms
7,040 KB
testcase_32 AC 104 ms
6,632 KB
testcase_33 AC 52 ms
5,456 KB
testcase_34 AC 78 ms
7,040 KB
testcase_35 AC 149 ms
7,040 KB
testcase_36 AC 107 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef int32_t i32;
typedef int64_t i64;

typedef struct cake {
  i32 a, b, c;
} node;

int cmp_node (const void *a, const void *b) {
  node *p = (node *)a;
  node *q = (node *)b;
  if (p->a != q->a) return p->a < q->a ? -1 : 1;
  return p->b > q->b ? -1 : 1;
}

int cmp_int (const void *a, const void *b) {
  i32 d = *(i32 *)a - *(i32 *)b;
  return d == 0 ? 0 : d < 0 ? -1 : 1;
}

i32 to_index (i32 *z, i32 n, i32 v) {
  i32 l = 0;
  i32 r = n;
  while (r - l > 1) {
    i32 m = (l + r) / 2;
    if (z[m] <= v) {
      l = m;
    } else {
      r = m;
    }
  }
  return l;
}

i64 max (i64 a, i64 b) {
  return a > b ? a : b;
}

void update (i64 *bit, i32 x, i64 v) {
  i32 n = bit[0];
  for (i32 i = x; i <= n; i += i & -i) {
    bit[i] = max (bit[i], v);
  }
}

i64 calc_max (i64 *bit, i32 x) {
  i64 res = 0;
  for (i32 i = x; i > 0; i -= i & -i) {
    res = max (res, bit[i]);
  }
  return res;
}

void run (void) {
  i32 n;
  scanf ("%" SCNi32, &n);
  node *p = (node *) calloc (n, sizeof (node));
  i32 *z = (i32 *) calloc (n, sizeof (i32));
  for (i32 i = 0; i < n; ++i) {
    i32 a, b, c;
    scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32, &a, &b, &c);
    p[i] = (node) {a, b, c};
    z[i] = b;
  }
  qsort (p, n, sizeof (node), cmp_node);
  qsort (z, n, sizeof (i32), cmp_int);
  i32 len = 1;
  for (i32 i = 1; i < n; ++i) {
    if (z[i] == z[len - 1]) continue;
    z[len++] = z[i];
  }
  i64 *bit = (i64 *) calloc (len + 1, sizeof (i64));
  bit[0] = len;
  for (i32 i = 0; i < n; ++i) {
    i32 k = to_index (z, len, p[i].b) + 1;
    i64 v = calc_max (bit, k - 1) + p[i].c;
    update (bit, k, v);
  }
  printf ("%" PRIi64 "\n", calc_max (bit, len));
}

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