满背包问题,把体积和价值看成相等的。用滚动数组优化,然后额外开辟一个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. Open Cygwin at a specific folder

    转自:https://stackoverflow.com/questions/9637601/open-cygwin-at-a-specific-folder# When you install Cy ...

  2. tf训练OTSU

    训练一个简单的回归网络 基础的函数如下: # coding=utf-8 import tensorflow as tf import numpy as np np.random.seed(0) # 卷 ...

  3. tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed(转)

    转载地址:https://www.cnblogs.com/youxin/p/8793554.html 使用方式: JAVA_OPTS="-Djava.awt.headless=true -D ...

  4. 如何将本地项目上传至GitHub(so easy!!!)

    如何将本地项目上传至GitHub 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直 ...

  5. halcon 创建region的最大尺寸问题

    gen_region 之类的创建region 之前需要提前设置region的最大尺寸,设置方法如下: set_system('width',2000)set_system('height',2000) ...

  6. Elasticsearch index fields 重命名

    reindex数据复制,重索引 POST _reindex { "source": { "index": "twitter" }, &quo ...

  7. 【ORACLE】解锁scott帐号

    sqlplus / as sysdba;SQL> alter user scott account unlock;SQL> conn scott/grace

  8. python中导入一个需要传参的模块

    最近跑实验,遇到了一个问题:由于实验数据集比较多,每次跑完一个数据集就需要手动更改文件路径,再将文件传到服务器,再运行实验,这样的话效率很低,必须要专门看着这个实验,啥时候跑完就手动修改运行下一个实验 ...

  9. 离屏Canvas — 使用Web Worker提高你的Canvas运行速度

    离屏Canvas — 使用Web Worker提高你的Canvas运行速度 原文链接: developers.google.com 现在因为有了离屏Canvas,你可以不用在你的主线程中绘制图像了! ...

  10. python 接口自动化测试(五)其他-认证&代理&超时配置

    有了前面几节的介绍,基本的接口测试是可以满足了.本节一些其它的高级技巧: 一.认证 1.基本认证: # -*- coding:utf-8 -*- import requests url = " ...