結果
問題 |
No.2869 yuusaan's Knapsacks
|
ユーザー |
![]() |
提出日時 | 2024-08-30 22:30:24 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 821 ms / 4,500 ms |
コード長 | 1,655 bytes |
コンパイル時間 | 9,534 ms |
コンパイル使用メモリ | 282,216 KB |
実行使用メモリ | 12,852 KB |
最終ジャッジ日時 | 2024-08-30 22:30:53 |
合計ジャッジ時間 | 14,955 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll,ll>; #define fix(x) fixed << setprecision(x) #define asc(x) x, vector<x>, greater<x> #define rep(i, n) for(int i = 0; i < n; ++i) #define all(x) (x).begin(),(x).end() template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;} template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;} constexpr ll INFLL = (1LL << 62), MOD = 998244353; constexpr int INF = (1 << 30); int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin >> n >> m; vector<int> e(n), v(m), w(m), tv(1<<m), tw(1<<m); rep(i,n) cin >> e[i]; rep(i,m) cin >> v[i] >> w[i]; reverse(all(e)); rep(i,1<<m){ ll totv = 0, totw = 0; rep(j,m){ if((i>>j)&1) totv += v[j], totw += w[j]; } tv[i] = totv; tw[i] = min<ll>(INF, totw); } vector<vector<int>> dp(n+1,vector<int>(1<<m,-INF)), par(n+1,vector<int>(1<<m,-1)); dp[0][0] = 0; rep(i,n){ rep(j,1<<m){ int mask = ((1<<m)-1) ^ j; for(int k=mask;;k=(k-1)&mask){ if(tw[k]>e[i]) continue; if(chmax(dp[i+1][j|k], dp[i][j]+tv[k])) par[i+1][j|k] = j; if(!k) break; } } } int state = -1, ans = -1; rep(i,1<<m){ if(chmax(ans,dp[n][i])) state = i; } cout << ans << '\n'; rep(i,n){ int x = par[n-i][state], y = x^state; cout << __builtin_popcount(y) << " "; rep(j,m) if((y>>j)&1) cout << j+1 << " "; cout << '\n'; state = x; } return 0; }