状压地爬做法 比较费空间,但是易懂
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,w,a[20];
int f[20][1<<19],tot;
int main()
{
scanf("%d%d",&n,&w);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[i][1<<(i-1)]=a[i];
}
memset(f,0x3f,sizeof(f));
f[1][0]=0;
tot=(1<<n)-1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=tot;j++)
{
if(f[i][j]!=0x3f3f3f3f)
{
for(int k=1;k<=n;k++)
{
if(w-f[i][j]>=a[k]&&!(j&1<<(k-1)))
{
f[i][j|1<<(k-1)]=min(f[i][j|1<<(k-1)],f[i][j]+a[k]);
}
else f[i+1][j|1<<(k-1)]=min(f[i+1][j|1<<(k-1)],a[k]);
}
}
}
if(f[i][tot]!=0x3f3f3f3f)
{
printf("%d",i);
return 0;
}
}
}
%%%
orz