結果

問題 No.2026 Yet Another Knapsack Problem
コンテスト
ユーザー yx l
提出日時 2026-11-24 21:43:16
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,550 ms / 10,000 ms
+ 496µs
コード長 2,584 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,321 ms
コンパイル使用メモリ 385,244 KB
実行使用メモリ 77,384 KB
最終ジャッジ日時 2026-11-24 21:43:30
合計ジャッジ時間 13,335 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//
//  main.cpp
//  abc
//
//  Created by lyx on 2024/mm/dd.
//

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#ifndef __APPLE__
#include <bits/stdc++.h>
#else
#include <iostream>
#include <algorithm>
#include <vector>
#endif
#define ll long long
#define ull unsigned long long
#define REP(i, l, r) for(int i = l; i <= r; ++i)
#define PER(i, r, l) for(int i = r; i >= l; --i)
using namespace std;

template<typename T> void chkmin(T &x, T y) { if (x > y) x = y; }
template<typename T> void chkmax(T &x, T y) { if (x < y) x = y; }

namespace Main {

const int N = 2505;
const int MAXC = 1255;
const ll INF = 1e18;

int n;
int c[N];
ll v[N];
ll dp[N][MAXC];
ll M[MAXC][N];
ll pref[MAXC][N];
int max_c[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	if (!(cin >> n)) return 0;

	REP(i, 1, n) cin >> c[i] >> v[i];

	ll v1 = v[1];
	int max_c_bound = n / 2;

	// 初始化 DP 数组
	REP(w, 0, n) {
		REP(cnt, 0, max_c_bound) dp[w][cnt] = -INF;
		max_c[w] = -1;
	}
	dp[0][0] = 0;
	max_c[0] = 0;

	int cur_max_w = 0;

	// 仅对 i >= 2 且 v[i] > v1 的高收益物品做背包
	REP(i, 2, n) {
		if (v[i] <= v1) continue;

		int eff_c = min(c[i], n / i);
		int rem = eff_c, p = 1;
		while (rem > 0) {
			int num = min(p, rem);
			rem -= num;
			int dw = num * i;
			int dc = num;
			ll val = num * v[i];

			int lim_w = min(cur_max_w, n - dw);
			PER(w, lim_w, 0) {
				int mc = max_c[w];
				if (mc == -1) continue;
				ll* src = dp[w];
				ll* dst = dp[w + dw];
				PER(cnt, mc, 0) {
					ll cur_val = src[cnt];
					if (cur_val > -INF / 2) {
						chkmax(dst[cnt + dc], cur_val + val);
					}
				}
			}
			PER(w, lim_w, 0) {
				if (max_c[w] != -1) {
					chkmax(max_c[w + dw], max_c[w] + dc);
				}
			}
			cur_max_w = min(n, cur_max_w + dw);
			p <<= 1;
		}
	}

	// 转换为超额增量收益 M[c][d]
	REP(cnt, 0, max_c_bound) {
		REP(d, 0, n) M[cnt][d] = -INF;
	}

	REP(w, 0, n) {
		int mc = max_c[w];
		REP(cnt, 0, mc) {
			if (dp[w][cnt] > -INF / 2) {
				int d = w - cnt;
				ll gain = dp[w][cnt] - 1LL * cnt * v1;
				chkmax(M[cnt][d], gain);
			}
		}
	}

	// 计算二维前缀最大值
	REP(cnt, 0, max_c_bound) {
		REP(d, 0, n) {
			ll val = M[cnt][d];
			if (cnt > 0) chkmax(val, pref[cnt - 1][d]);
			if (d > 0) chkmax(val, pref[cnt][d - 1]);
			pref[cnt][d] = val;
		}
	}

	// O(1) 输出 k = 1 .. N 的答案
	REP(k, 1, n) {
		int qc = min(k, max_c_bound);
		int qd = n - k;
		ll ans = 1LL * k * v1 + pref[qc][qd];
		cout << ans << '\n';
	}

	return 0;
}

}

int main() {
	Main :: main();
	return 0;
}
0