結果
問題 |
No.2026 Yet Another Knapsack Problem
|
ユーザー |
👑 ![]() |
提出日時 | 2022-07-29 23:28:11 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 368 ms / 10,000 ms |
コード長 | 1,285 bytes |
コンパイル時間 | 3,501 ms |
コンパイル使用メモリ | 127,052 KB |
最終ジャッジ日時 | 2025-01-30 15:47:42 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <tuple> using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; i64 dp[2501][2501]; int main(){ int N; cin >> N; vector<tuple<i64,i64,i64,i64>> wWVD; rep(i,N){ i64 w, c, v; w = i+1; cin >> c >> v; if(c >= N/w){ int C=1; while(C<c){ C*=2; } c = C; } for(i64 d=1; d<=c; d*=2){ wWVD.push_back({w,w*d,v*d,d}); c -= d; if(d&c){ wWVD.push_back({w,w*d,v*d,d}); c -= d; } } } sort(wWVD.rbegin(), wWVD.rend()); rep(i,N+1) rep(j,N+1) dp[i][j] = -INF; rep(i,N+1) dp[0][i] = 0; for(auto [w,W,V,D] : wWVD){ i64 K = N / w; for(i64 k=K; k>=D; k--){ for(i64 i=W; i<=N; i++){ dp[k][i] = max(dp[k][i], dp[k-D][i-W] + V); } } } for(int k=1; k<=N; k++) cout << dp[k][N] << '\n'; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } ios_do_not_sync_instance;