Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
141870 陈志轩 简单背包问题 C++ 通过 100 1 MS 1128 KB 1661 2024-04-06 14:23:44

Tests(5/5):


#include<bits/stdc++.h> #define int long long using namespace std; namespace Fast{ inline int fr(){ register int x = 0,f = 1; static char c = getchar(); while (c < '0' || c > '9'){ if (c == '-'){ f = -1; } c = getchar(); } while (c >= '0' && c <= '9'){ x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void fw(int x){ if (x == 0){ putchar('0'); return ; } if (x < 0){ putchar('-'); x = -x; } stack <char> s; while (x){ s.push(x % 10 + 48); x /= 10; } while (!s.empty()){ putchar(s.top()); s.pop(); } } } using namespace Fast; int a[35],dp[35][1005]; vector <int> pd[35][1005]; signed main(){ int s,n; s = fr(),n = fr(); for (int i = 1;i <= n;i++){ a[i] = fr(); } for (int i = 1;i <= n;i++){ for (int j = 0;j <= s;j++){ dp[i][j] = dp[i - 1][j]; if (j >= a[i]){ int x = dp[i - 1][j - a[i]] + a[i]; //dp[i][j] = max(dp[i][j],dp[i - 1][j - a[i]] + a[i]); if (x > dp[i][j]){ dp[i][j] = x; for (int k = 0;k < pd[i - 1][j - a[i]].size();k++){ pd[i][j].push_back(pd[i - 1][j - a[i]][k]); } pd[i][j].push_back(a[i]); } else{ for (int k = 0;k < pd[i - 1][j].size();k++){ pd[i][j].push_back(pd[i - 1][j][k]); } } } else{ for (int k = 0;k < pd[i - 1][j].size();k++){ pd[i][j].push_back(pd[i - 1][j][k]); } } } } //cout<<dp[n][s]<<'\n'; if (dp[n][s] != s){ cout<<"Failed!"; return 0; } for (int i = 0;i < pd[n][s].size();i++){ cout<<pd[n][s][i]<<'\n'; } return 0; }


测评信息: