[Noip1999] design of the stamp nominal value DFS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.