1 条题解
-
-3
#include<bits/stdc++.h> using namespace std; const int N = 1100; int w[N]; long long dp[2][1010*100]; bool path[N][N]; int ans[N], cnt; int main(){ freopen("cards.in","r",stdin); freopen("cards.out","w",stdout); int Tw , n; cin >> Tw >> n; int sum = 0; for (int i = 1; i <= n; i ++){ cin >> w[i]; sum += w[i]; } sum -= Tw; dp[0][0] = 1; bool flag = 1; for (int i = 1; i <= n; i++){ for(int j = 0; j <= sum; j ++){ if(j >= w[i]){ dp[flag][j] = dp[1-flag][j] + dp[1-flag][j-w[i]]; if(dp[1-flag][j-w[i]]){ path[i][j] = true; } }else{ dp[flag][j] = dp[1-flag][j]; } } flag = 1 - flag; } if(dp[1-flag][sum] > 1) cout << -1 << endl; else if(!dp[1-flag][sum]) cout << 0 << endl; else{ for (int i = n; i >= 1; i --){ if(path[i][sum]){ ans[cnt++] = i; sum -= w[i]; } } for (int i = cnt - 1; i >= 0; i --){ cout << ans[i] << " "; } } return 0; }
信息
- ID
- 506
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 184
- 已通过
- 21
- 上传者