The data range is terrible, but it still needs to run for 400 ms.
I will not talk much about the meaning of the question. I will understand the question after reading the example, and I will talk about the general idea of solving the problem.
DFS should have no doubt, that is, input parameters based on m as the maximum item type, and then enumerate the item size one by one. You can use a backpack to check.
But how can we enumerate the value of the added item? Where is the upper limit? Therefore, we may first run the backpack before enumeration and then determine that maxm can be obtained at the maximum continuity. If the value of the newly added item is greater than that of maxm + 1, a fault will obviously occur, so we can use maxm + 1 as the upper bound of the enumeration, and then perform the next layer of DFS.
At the same time, you can also find the answer in the process of determining the upper bound of enumeration, and the code is still very easy to write and look good.
#include <cstdio>#include <cstring>#include <algorithm>#define N 500using namespace std;int n,m,ans;int a[N],b[N],f[N];inline void dfs(int deep){int i,j,uplimit;memset(f,0x3f,sizeof(f));f[0]=0;for(uplimit=1;uplimit<=N;uplimit++){for(i=1;i<=deep&&a[i]<=uplimit;i++)f[uplimit]=min(f[uplimit],f[uplimit-a[i]]+1);if(f[uplimit]>n){uplimit--;if(uplimit>ans){ans=uplimit;for(i=1;i<=deep;i++)b[i]=a[i];}break;}}if(deep==m)return ;for(i=uplimit+1;i>a[deep];i--){a[deep+1]=i;dfs(deep+1);}}int main(){//freopen("test.in","r",stdin);int i,j,k;scanf("%d%d",&n,&m);a[1]=1;dfs(1);for(int i=1;i<=m;i++)printf("%d ",b[i]);printf("\nMAX=%d\n",ans);}
[Noip1999] design of the stamp nominal value DFS