邮票面值设计 (动态规划+DFS)

本文详细解析了如何运用深度搜索(DFS)结合动态规划(DP)解决硬币组合问题,旨在寻找k种不同面值的硬币,使得能够用不超过n张硬币表示出尽可能大的数值。文章提供了具体实现代码,包括剪枝策略以提高搜索效率,以及详细的代码注释帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:https://ac.nowcoder.com/acm/problem/16813

思路:

  深度搜索:每一层枚举一个面值,然后通过dp进行检查,并通过已知面值得到最多n张得到的最大表示数。

       其实,该搜索就是一个比较裸的,进行剪枝,枚举的面值还是存在范围的,上一次面值+1~n*sum(sum表示所有已知面值相加,其实这只是一个粗糙的剪枝,但是,对于我这种弱鸡莱来说还是香)

更多,细节的解释还是在代码里。还有,有多余的输出,需要自己去删除。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n, k, res, ans[105], f[2005], curans[105];
int solve(int dep, int sum){
    memset(f, 0x3f, sizeof(f));
    f[0] = 0;
    for (int i = 1; i <= dep; ++i){
        for (int j = curans[i]; j <= n*sum; ++j)            //完全背包,f[i]记录的是在已知的面值中选择最少数量
            f[j] = min(f[j], f[j - curans[i]] + 1);            //如果最少数量大于n时,说明i这个数字不能表示,但是
    }                                                        //i-1表示可表示的最大值。
    for (int i = 1; i <= n*sum; ++i){
        if (f[i] > n){
            return i - 1;
        }
    }
    return n*sum;
}

void dfs(int dep, int last, int maxn, int sum){
    if (dep > k){                                        //dep作为递归结束,maxn是一个估计上界(因为不能无限
        if (res < maxn){                                //枚举下去,但是面值肯定小于n*sum)
            res = maxn;                                    //而枚举的下界就是i+1
            for (int i = 1; i <= k; ++i){
                ans[i] = curans[i];
            }
        }
        return;
    }
    for (int i = last + 1; i <= maxn + 1; ++i){
        curans[dep] = i;
        int x = solve(dep, sum + i);
        cout << "dep=" << dep<<endl;
        cout << "maxn=" << maxn << "   sum+i=" << sum + i << endl;
        cout << "x=" << x << endl;
        for (int i = 1; i <= dep; ++i)
            cout << "cur[" << i << "]="<<curans[i] << endl;
        cout << endl << endl;
        dfs(dep + 1, i, x, sum + i);
    }
}

int main(){
    cin >> n >> k;
    dfs(1, 0, 0, 0);
    for (int i = 1; i <= k; ++i)
        cout << ans[i] << " ";
    cout << endl << "MAX=" << res << endl;
}

 

转载于:https://www.cnblogs.com/ALINGMAOMAO/p/10355545.html

DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
好的,下面是使用C++编程实现回溯法求解连续邮资问题的代码: ```c++ #include <iostream> #include <vector> using namespace std; int max_stamp; // 最大连续邮资 vector<int> stamps, used; // 邮票面值和已选邮票 int n, m; // 面值种类数和可贴邮票数 // 检查已选面值是否构成连续的整数序列 bool check() { int sum = 0; for (int i = 0; i < m; i++) { sum += used[i]; } for (int i = 1; i < m; i++) { if (used[i] - used[i-1] != 1) { return false; } } max_stamp = max(max_stamp, sum); return true; } // 回溯搜索 void dfs(int u) { if (used.size() == m) { check(); return; } if (u == n) { return; } dfs(u+1); // 不选当前邮票 used.push_back(stamps[u]); dfs(u+1); // 选择当前邮票 used.pop_back(); } int main() { cin >> n >> m; stamps.resize(n); for (int i = 0; i < n; i++) { cin >> stamps[i]; } dfs(0); cout << max_stamp << endl; return 0; } ``` 在上述代码中,check() 函数用于检查已选面值是否构成连续的整数序列,如果满足条件,则更新最大连续邮资。dfs() 函数用于进行回溯搜索,其中 u 表示当前需要选取的邮票面值,如果已选面值个数达到可贴邮票数目,则调用 check() 函数进行检查。在 main() 函数中,首先读入面值种类数和可贴邮票数,然后读入每个邮票面值。最后调用 dfs() 函数进行搜索,并输出最大连续邮资。 需要注意的是,在实际应用中,需要对回溯法进行剪枝和优化,以提高算法效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值