結果

問題 No.2167 Fibonacci Knapsack
ユーザー norikamenorikame
提出日時 2022-12-21 23:27:16
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,322 bytes
コンパイル時間 5,375 ms
コンパイル使用メモリ 314,904 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 11:03:44
合計ジャッジ時間 7,617 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

// 

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const int INF = (int)(1e9);

int main() {
	int t0;
	cin >> t0;
	rep(i0, t0) {
		int n;
		ll w;
		cin >> n >> w;
		vector<ll> wi(n), fib(n);
		rep(i, n) cin >> wi[i];
		rep(i, n) {
			if (i == 0) fib[i] = 1;
			else if (i == 1) fib[i] = 2;
			else fib[i] = fib[i-2] + fib[i-1];
		}
		map<ll, ll, greater<ll>> vals;
		vals[0] = 0;
		repr(i, n) {
			auto nvals = vals;
			for (const auto& pi : vals) if (pi.second+wi[i] <= w) {
				if (nvals.find(pi.first+fib[i]) == nvals.end()) nvals[pi.first+fib[i]] = pi.second+wi[i];
				else chmin(nvals[pi.first+fib[i]], pi.second+wi[i]);
			}
			if ((int)(nvals.size()) > 2) nvals.erase(next(nvals.begin(), 2), nvals.end());
			swap(nvals, vals);
		}
		cout << vals.begin()->first << endl;
	}
	return 0;
}
0