結果
| 問題 |
No.2026 Yet Another Knapsack Problem
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2022-07-29 23:44:27 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 284 ms / 10,000 ms |
| コード長 | 1,377 bytes |
| コンパイル時間 | 5,648 ms |
| コンパイル使用メモリ | 135,536 KB |
| 最終ジャッジ日時 | 2025-01-30 15:50:58 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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;
Nachia