类似背包 f[i][sum] |= f[i-1][sum-a[i] ] ,这里设置为1或-1
#include <bits/stdc++.h> using namespace std ; const int N=2e4+10; const int D=1e4; int n,m,a[N],f[102][N],ans[N] ; void out() { int s=D+m; for(int i=n; i>=2; i--){ ans[i]=f[i][s]; if(ans[i]==1) s-=a[i]; else if(ans[i]==-1) s+=a[i]; } int cnt=0; for(int i=2; i<=n; i++) if(ans[i]==1) { cout<<i-cnt-1<<endl; cnt++; } for(int i=2; i<=n; i++) if(ans[i]==-1) cout<<1<<endl; } void solve(){ int i,j; for(i=1;i<=n;i++)cin>>a[i]; f[1][a[1]+D]=1; f[2][D+a[1]-a[2]]=-1; for(i=3;i<=n;i++) for(j=-1e4+D;j<=1e4+D;j++) if(f[i-1][j]){ f[i][j+a[i]]=1; f[i][j-a[i]]=-1 ; } } signed main(){ cin>>n>>m; solve(); out(); }
标签:const,int,sum,316,ans,操作,acwing,out From: https://www.cnblogs.com/towboa/p/17137768.html