[pat]1068 Find More Coins
满背包问题,把体积和价值看成相等的。用滚动数组优化,然后额外开辟一个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的更多相关文章
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- 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 ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- PAT甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- PAT 甲级 1068 Find More Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...
- PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...
- 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)
题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...
- 1068. Find More Coins (30)
题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
随机推荐
- Git入门到高级系列1-git安装与基础命令
视频课程地址 腾讯课堂 为什么要进行项目文件的版本管理 代码备份和恢复 团队开发和协作流程 项目分支管理和备份 git 是什么? git是一个分布式的版本控制软件.版本控制是一种记录一个或若干文件内容 ...
- 异类查询要求为连接设置ANSI_NULLS和ANSI_WARNINGS选项
在查询分析器中,先输入两句 set ansi_nulls on set ansi_warnings on 执行然后再 Create Proc 存储过程 ...
- Google API Design Guide (谷歌API设计指南)中文版
面向资源的设计 这份设计指南的目标是帮助开发人员设计简单.一致.易用的网络API.同时,它也有助于收敛基于socket的API和(注:原文是with,这里翻译为“和”)基于HTTP的REST API. ...
- Android打开doc、xlsx、ppt等office文档解决方案
妹子我写代码很辛苦/(ㄒoㄒ)/~~ ,转载请标明出处哦~http://blog.csdn.net/u011791526/article/details/73088768 1.Android端有什么控 ...
- css背景图宽度只适应,高度不变
保证1920px的图片,在低分率率的电脑上也能正常显示,两边裁剪,中间居中,高度不变 <!DOCTYPE html> <html lang="en"> &l ...
- android控件RecyclerView中,如何显示自定义分割线以及最后一项去除分割线
在控件RecyclerView中,分割线DividerItemDecoration类的使用经常见,如果是使用自带的分割线,只需要这样写即可 RecyclerView mRecyclerView; mR ...
- 从unity丢图标到unity进不去桌面
现象1: 用了一年多的unity的右上角的网络图标和网易云音乐的图标消失不见了,我也不记得最近有update或upgrade过,然而这两个功能还是可以正常用 解决1: 安装Gnome,果然相应的图标就 ...
- git error: Your local changes to the following files would be overwritten by merge:xxxxxx ,Please commit your changes or stash them before you merge.的phpstorm解决办法
git报错 error: Your local changes to the following files would be overwritten by merge: .idea/encoding ...
- webpack简介与使用
欢迎小伙伴们为 前端导航仓库 点star https://github.com/pfan123/fr...前端导航平台访问 CommonJS 和 AMD 是用于 JavaScript 模块管理的两大规 ...
- Tomcat中的Listener源码片段解读
@Override public <T extends EventListener> void addListener(T t) { if (!context.getState().equ ...