結果

問題 No.2167 Fibonacci Knapsack
ユーザー 👑 chro_96chro_96
提出日時 2022-12-19 01:21:15
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 28,740 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 09:23:30
合計ジャッジ時間 2,575 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void func (int n, int prev_flag, long long tmp, long long w_all, long long *w, long long *fib, long long *ans) {
  if (n <= 0) {
    if (tmp > *ans) {
      *ans = tmp;
    }
    return;
  }
  
  if (tmp+fib[n+1]-2LL <= *ans) {
    return;
  }
  
  n--;
  if (w[n] <= w_all) {
    func(n, 1, tmp+fib[n], w_all-w[n], w, fib, ans);
    return;
  }
  
  if (prev_flag > 0 && n > 0 && w[n]+w[n-1] < w[n+1] && w[n-1] > w_all) {
    func(n, 1, tmp-fib[n+1]+fib[n], w_all+w[n+1]-w[n], w, fib, ans);
    return;
  }
  
  func(n, 0, tmp, w_all, w, fib, ans);
  if (prev_flag > 0 && w[n] <= w_all+w[n+1]) {
    func(n, 1, tmp-fib[n+1]+fib[n], w_all+w[n+1]-w[n], w, fib, ans);
  }
  
  return;
}

int main () {
  int t = 0;
  int n = 0;
  long long w_all = 0LL;
  long long w[75] = {};
  
  int res = 0;
  
  long long fib[77] = {};
  
  fib[0] = 1LL;
  fib[1] = 2LL;
  for (int i = 2; i < 77; i++) {
    fib[i] = fib[i-1]+fib[i-2];
  }
  
  res = scanf("%d", &t);
  while (t > 0) {
    long long ans = 0LL;
    res = scanf("%d", &n);
    res = scanf("%lld", &w_all);
    for (int i = 0; i < n; i++) {
      res = scanf("%lld", w+i);
    }
    func(n, 0, 0LL, w_all, w, fib, &ans);
    printf("%lld\n", ans);
    t--;
  }
  
  return 0;
}
0