满背包问题,把体积和价值看成相等的。用滚动数组优化,然后额外开辟一个choice数组来记录每次的选择,然后回溯打印。因为要按字典序,先把价值进行排序。假如选最小的商品能装满m的话,那就把判断条件改成大于等于,然后最后来

#include<bits/stdc++.h>
using namespace std;
int dp[];
int w[];
bool flag[];
bool choice[][];
int n, m;
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
memset(flag, false, sizeof(flag));
fill(choice[], choice[] +, false);
fill(dp, dp +, );
scanf("%d %d", &n, &m);
int i, j;
for (i = ; i <= n; i++)
{
scanf("%d", &w[i]);
}
sort(w+, w + n+,cmp);
for (i = ; i <= n; i++)
{
for (j = m; j >= w[i]; j--)
{
if (dp[j-w[i]]+w[i]>=dp[j])
{
dp[j] = dp[j - w[i]] + w[i];
choice[i][j] = true;
}
}
}
int x=n, y=m;
int num = ;
if (dp[m] != m)
{
printf("No Solution\n");
}
else
{
while (x >= )
{
if (choice[x][y] == true)
{
flag[x] = true;//下标
num++;
y -= w[x];
}
x--;
}
for (i = n; i >= ; i--)
{
if (flag[i] == true)
{
if (num - > )
{
printf("%d ", w[i]);
num--;
}
else
printf("%d\n", w[i]);
}
}
}
}

选择最小的那个。

[pat]1068 Find More Coins的更多相关文章

  1. PAT 1068 Find More Coins[dp][难]

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

  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 (30 分) (dp,01背包问题记录最佳选择方案)***

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

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

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

  5. PAT甲级1068 Find More Coins【01背包】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...

  6. PAT 甲级 1068 Find More Coins

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

  7. PAT (Advanced Level) 1068. Find More Coins (30)

    01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...

  8. 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

    题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...

  9. 1068. Find More Coins (30)

    题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

随机推荐

  1. YAML文件格式_k8s/docker-compose配置文件

    YAML(Yet Another Markup Language),是一个JSON的超集,意味着任何有效JSON文件也都是一个YAML文件.它规则如下: )大小写敏感 )使用缩进表示层级关系,但不支持 ...

  2. office2007每次打开都要配置文件,怎么取消配置(可行)

    取消打开软件进行配置的方法 打开“我的电脑”——“C盘”— —“Program Files\Common Files\ Microsoft Shared\OFFICE12\Office Setup C ...

  3. Codeforces Round #313 (Div. 2) C. Gerald&#39;s Hexagon(补大三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Vuex详解

    一.什么是Vuex 官网解释如下: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex ...

  5. DirectX using C++_error X3539:ps1_x is no longer supported...解决方案

    问题来源 在研究HLSL时编译一个demo出现了error X3539的问题 解决方案 将代码中的ps_1_1 改为ps_2_0 PixelShader = compile ps_1_1 PS(); ...

  6. “Java是编译执行的语言”这句话对吗?

    现在让你谈谈对Java平台的理解,你是否会感觉内容过于庞大?这个问题是比较宽泛的,Java发展到现在已经不仅仅是语言这么简单了,Java平台涉及的,包括但不仅限于下面提到的这些内容: Java语言本身 ...

  7. CentOS 6.8 防火墙配置

    系统: CentOS release 6.8 (Final) iptables v1.4.7 执行命令: #清除所有规则 iptables -F #开放redis端口 iptables -A INPU ...

  8. Windows10 + IntelliJ IDEA 2017.3.2 + wamp2e + xdebug 调试 配置

    一.环境 系统: windows10 WampServer:  wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe IDE:  Intel ...

  9. weblogic 整合cxf 报错:cannot create a secure XmlInputFactory

    weblogic 整合cxf 报错:cannot create a secure XmlInputFactory ================================ ©Copyright ...

  10. [git/GitHub] git push 时报错:fatal: remote error: You can't push to git://github.com/user/xxx.git(已解决)

    当使用  git push  时,提示以下错误: fatal: remote error: You can't push to git://github.com/user/xxx.git Use ht ...