题目https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976

题意

n个硬币,每一个有一个特有的价值,一个硬币只有一个,要求选取一些硬币使得他们的价值刚好是m

输出字典序最小的方案。

思路:

最近好久没有刷题了连PAT都好久没动了这样不行啊。连背包都有点不大会了。赶紧把PAT30分的刷完去刷洛谷了。

硬币是一个weight和value相同的物品,背包的容量就是m

问题转换成尽量让包中的价值大,背包中的最大价值都无法到达m说明没有答案。

价值是不可能超过m的,因为weight和value相同,weight限制了。

用一个bool二维数组来保存某一个方案中有没有这个硬币。

因为要求输出字典序最小的方案,所以刚开始先从大到小来排序,先考察大的,当小的和当前价值一样的时候也进行更新。

这时候的更新就是将更小的一个方案进行更新。

在逆序跑bool数组存方案就行了。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, m;
const int maxn = 1e4 + ;
int val[maxn];
bool choice[maxn][];
int dp[maxn]; bool cmp(int a, int b){
return a > b;
} int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++){
scanf("%d", &val[i]);
}
sort(val, val + n, cmp);
for(int i = ; i < n; i++){
for(int j = m; j >= val[i]; j--){
if(dp[j] <= dp[j - val[i]] + val[i]){
choice[i][j] = true;
dp[j] = dp[j - val[i]] + val[i];
}
}
}
if(dp[m] != m)printf("No Solution\n");
else{
vector<int>ans;
int now = m, id = n - ;
while(now > ){
if(choice[id][now]){
ans.push_back(val[id]);
now -= val[id];
}
id--;
}
printf("%d", ans[]);
for(int i = ; i < ans.size(); i++){
printf(" %d", ans[i]);
}
printf("\n");
}
return ;
}

PAT甲级1068 Find More Coins【01背包】的更多相关文章

  1. PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

    1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some ...

  2. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

  3. PAT 甲级 1068 Find More Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...

  4. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

  5. UVA 562 Dividing coins (01背包)

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  6. uva562 Dividing coins 01背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. PAT甲级——A1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  8. 【PAT甲级】1048 Find Coins (25 分)(二分)

    题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...

  9. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

随机推荐

  1. TensorFlow迭代速度变慢的问题

    最近用TensorFlow实现遗传算法(Genetic Algorithms),发现迭代速度越来越慢,用time.time()观察以后,发现每次迭代都要比上一次慢0.5秒左右,但是每次迭代的计算量是差 ...

  2. 转:C# Delegate委托 1

    Delegate中文翻译为“委托”.MSDN中对Delegate的解释如下: C#中的委托类似于C或C++中的函数指针.使用委托使程序员可以将方法引用封装在委托对象内.然后可以将该委托对象传递给可调用 ...

  3. 连接postgres特别消耗cpu资源而引发的PostgreSQL性能优化考虑

    由于是开发阶段,所以并没有配置postgres的参数,都是使用安装时的默认配置,以前运行也不见得有什么不正常,可是前几天我的cpu资源占用突然升高.查看进程,发现有一个postgres的进程占用CPU ...

  4. 解决python3环境下twisted安装问题

    twisted的安装经常会出问题,提示: 1.twisted错误,报Unable to find vcvarsall.bat2.等 解决办法: 1.安装wheel,命令:pip install whe ...

  5. TensorFlow精选Github开源项目

    转载于:http://www.matools.com/blog/1801988 TensorFlow源码 https://github.com/tensorflow/tensorflow 基于Tens ...

  6. [elk]elasticsearch实现冷热数据分离

    本文以最新的elasticsearch-6.3.0.tar.gz为例,为了节约资源,本文将副本调为0, 无client角色 https://www.elastic.co/blog/hot-warm-a ...

  7. 试试SQLServer 2014的内存优化表

    SQL Server2014存储引擎:行存储引擎,列存储引擎,内存引擎 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据 ...

  8. vim简单的移动光标

    vim学习简单的光标移动 ==> 移动到行首 $ ==> 移动到行尾 ^ ==> 移动到本行第一个不是blank的位置 g_ ==> 移动到本行最后一个不是blank的位置^Z ...

  9. redhat杂记

    1.设置sudo权限:修改/etc/sudoers文件,找到root    ALL=(ALL)       ALL,在后面添加nginx ALL=(ALL) NOPASSWD: ALL 2.用sed命 ...

  10. Mysql 5.7 安装 解压缩版 忘记密码

    [参考]Mysql 命令大全 [参考]MySQL 5.7.19 忘记密码 重置密码 配置文件my.ini示例 服务启动后停止 log配置 [参考]MySQL5.7.19 服务挂掉 自动关闭 mysql ...