vijosP1159 岳麓山上打水

vijosP1159 岳麓山上打水

 

链接:https://vijos.org/p/1159

 

【思路】

   迭代加深搜索+完全背包判断。

   自己没有思路,看的别人代码。

   总体上讲就是不断增大桶的数目并以之为上界搜索,用DP判断搜索是否可行。貌似数据很水所以可以较快通过。

   其中DFSID(cur+1,dep)很奇妙,如果改成取i从0到n的搜索的话会TLE。

 

【代码】

 

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 100+10;
 8 
 9 int dp[20010];
10 int ans[maxn],a[maxn];
11 int n,q,max_d;
12 
13 int check(int sum)
14 {
15     int &cur=dp[sum];
16     if(cur != -1) return cur;
17     if(sum==0) return cur=1;
18     else 
19     {
20         for(int i=0;i<max_d;i++)
21           if(sum>=ans[i] && check(sum-ans[i]))
22              return cur=1;
23     }
24     return cur=0;
25 }
26 
27 void DFSID(int cur,int dep) {
28     if(dep==max_d)                   //到达深度限制判断是否能够组成q 
29     {
30         memset(dp,-1,sizeof(dp));
31         if(check(q))
32         {
33             cout<<max_d<<" ";
34             for(int i=0;i<dep;i++) cout<<ans[i]<<" ";
35             exit(0);
36         }
37         return ;  //return 
38     }
39     
40     if(cur>=n) return ;
41     
42     ans[dep]=a[cur];
43     DFSID(cur+1,dep+1);
44     DFSID(cur+1,dep);   //一个不错的写法 
45 }
46 
47 int main() {
48     ios::sync_with_stdio(false);
49     
50     cin>>q>>n;
51     for(int i=0;i<n;i++) cin>>a[i];
52     
53     sort(a,a+n);         //字典序要求最小 
54     
55     for(max_d=1;max_d<=n;max_d++)
56        DFSID(0,0);
57     
58     return 0;
59 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值