結果

問題 No.2167 Fibonacci Knapsack
ユーザー 👑 rin204rin204
提出日時 2022-12-19 00:29:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,146 bytes
コンパイル時間 2,330 ms
コンパイル使用メモリ 209,752 KB
実行使用メモリ 4,660 KB
最終ジャッジ日時 2023-08-11 09:22:30
合計ジャッジ時間 10,863 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 314 ms
4,384 KB
testcase_04 AC 307 ms
4,376 KB
testcase_05 AC 359 ms
4,660 KB
testcase_06 AC 330 ms
4,384 KB
testcase_07 AC 385 ms
4,380 KB
testcase_08 AC 394 ms
4,440 KB
testcase_09 AC 343 ms
4,504 KB
testcase_10 AC 350 ms
4,620 KB
testcase_11 AC 356 ms
4,380 KB
testcase_12 AC 380 ms
4,384 KB
testcase_13 AC 371 ms
4,568 KB
testcase_14 AC 359 ms
4,388 KB
testcase_15 AC 324 ms
4,440 KB
testcase_16 AC 373 ms
4,380 KB
testcase_17 AC 347 ms
4,436 KB
testcase_18 AC 324 ms
4,380 KB
testcase_19 AC 330 ms
4,376 KB
testcase_20 AC 366 ms
4,452 KB
testcase_21 AC 362 ms
4,400 KB
testcase_22 AC 350 ms
4,516 KB
testcase_23 AC 247 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'

void solve(){
	int n;
	ll W;
	cin >> n >> W;
	if(n == 1){
		ll w;
		cin >> w;
		if(w <= W) cout << 1 << endl;
		else cout << 0 << endl;
		return;
	}
	vector<ll> w(n), F(n);
	F[0] = 1;
	F[1] = 2;
	for(int i = 0; i < n; i++){
		cin >> w[i];
		if(i >= 2) F[i] = F[i - 1] + F[i - 2];
	}

	reverse(w.begin(), w.end());
	reverse(F.begin(), F.end());

	map<pair<int, ll>, ll> memo;

	auto dfs=[&](auto self, int i, ll W) -> ll {
		if(i == n) return 0;
		else if(i == n - 1){
			if(W >= w[i]) return 1LL;
			else return 0LL;
		}

		if(memo.count({i, W})) return memo[{i, W}];

		if(w[i] > W){
			return self(self, i + 1, W);
		}
		else if(w[i + 1] > W){
			return self(self, i + 1, W - w[i]) + F[i];
		}
		else if(w[i] + w[i + 1] <= W){
			return self(self, i + 1, W - w[i]) + F[i];
		}
		ll ret = max(self(self, i + 2, W - w[i]) + F[i], self(self, i + 2, W - w[i + 1]) + F[i + 1]);
		memo[{i, W}] = ret;
		return ret;
	};

	ll ans = dfs(dfs, 0, W);
	cout << ans << endl;
}


int main(){
	cin.tie(0)->sync_with_stdio(0);

	int t = 1;
	cin >> t;
	while(t--) solve();
}
0