1 条题解

  • 0
    @ 2025-2-14 21:20:42

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int m,n,k,v,tot;
    int a[221],ans[221];
    int many[20005],which[20005],pre[20005];
    void init();
    void work();
    int main()
    {
    	//freopen("water.in","r",stdin);
    	//freopen("water.out","w",stdout);
    	init();
    	work();
    	//fclose(stdin);
    	//fclose(stdout);
    	return 0;
    }
    void init()
    {
    	cin>>v;
    	for(int i=1;i<=v;i++) many[i]=2000;
    	cin>>n;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	sort(a+1,a+n+1);
    }
    void work()
    {
    	int t;
    	many[0]=0;
    	for(int i=1;i<=n;i++)
    		for(int j=0;j<=v-a[i];j++)
    			if(many[j]!=2000 && which[j]!=a[i])
    			for(int k=1;k<=(v-j)/a[i];k++)
    			{
    				t=a[i]*k+j;
    				if(many[j]+1==many[t])
    					if(which[j]<which[pre[t]])
    					{
    						which[t]=a[i];
    						pre[t]=j;
    					}
    				if(many[j]+1<many[t])
    				{
    					many[t]=many[j]+1;
    					which[t]=a[i];
    					pre[t]=j;
    				}
    			}
    		cout<<many[v];
    		t=v;
    		while(t!=0)
    		{
    			tot++;
    			ans[tot]=which[t];
    			t=pre[t];
    		}
    		for(int i=tot;i>0;i--) cout<<' '<<ans[i];
    }
    
    • 1

    信息

    ID
    852
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者