PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10^4^ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=10^4^, the total number of coins) and M(<=10^2^, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the face values V~1~ <= V~2~ <= ... <= V~k~ such that V~1~ + V~2~ + ... + V~k~ = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.
Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].
Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution
//居然都没看出来这是个01背包问题,我对动态规划真的是不敢去做,好难啊。
//代码来自:https://www.liuchuo.net/archives/2323
#include <iostream>
#include <vector>
#include<stdio.h>
#include <algorithm>
using namespace std;
int dp[], w[];
bool choice[][];
int cmp1(int a, int b){return a > b;}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%d", &w[i]);
sort(w + , w + n + , cmp1);
for(int i = ; i <= n; i++) {
for(int j = m; j >= w[i]; j--) {//j的范围表示至少得放下自己。
if(dp[j] <= dp[j-w[i]] + w[i]) {
choice[i][j] = true;
printf("%d %d",i,j);
dp[j] = dp[j-w[i]] + w[i];
}
}
printf("\n");
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
printf("%d ",choice[i][j]);
}
printf("\n");
} if(dp[m] != m) printf("No Solution");
else {
vector<int> arr;
int v = m, index = n;
//由大到小排列,那么从小(n)开始遍历。
while(v > ) {
if(choice[index][v] == true) {
arr.push_back(w[index]);
v -= w[index];
}
index--;
}
for(int i = ; i < arr.size(); i++) {
if(i != ) printf(" ");
printf("%d", arr[i]);
}
}
return ;
}
//这个代码可以说写的超级棒了。
1.将元素不是从小到大排序,而是从大到小排序。
2.使用01背包来解决问题,i用来遍历所有的物品,j用来表示数量,并且最小是不超过本物品的重量大小。
3.choice数组记录物品i在重量为j的情况下是否被选中,并且倒序遍历,好厉害啊。
给的例子中的choice数组。非常厉害,学习了01数组。
PAT 1068 Find More Coins[dp][难]的更多相关文章
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- [pat]1068 Find More Coins
满背包问题,把体积和价值看成相等的.用滚动数组优化,然后额外开辟一个choice数组来记录每次的选择,然后回溯打印.因为要按字典序,先把价值进行排序.假如选最小的商品能装满m的话,那就把判断条件改成大 ...
- 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(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)
题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...
随机推荐
- 五、K3 WISE 开发插件《K3 Wise 群发短信配置开发(二)之短信群发配置》
开发环境:K/3 Wise 13.0.Sql Server 2005 目录 一.开启Sql Server Agent代理服务 二.短信发送原理 三.编写存储过程 四.开启Sql Server作业 一. ...
- Android SDK更新8.1.0时报错
Done loading packages.Preparing to install archivesDownloading SDK Platform Android 8.1.0, API 27, r ...
- 国产手机插入mac os 系统中无法被识别的解决方法
一些国产手机插入mac os 系统中无法被识别,在命令行输入 system_profiler SPUSBDataType在, 然后将魅蓝note的vendor id 添加至 ~/.android/ad ...
- Windows平台编译SQLite 3
由于需要sqlite的x64版本只能自己编译,下载sqlite源代码.sqlite.def.Visual Studio 2013新建一个Visual C++ Empty Project,Configu ...
- 问题记录 为ubuntu16.04添加windows字体(解决JIRA图表乱码的问题)
最近遇到了JIRA在新的ubuntu机器上图表的中文无法正确显示的问题,解决的方法是,为ubuntu安装中文字体,我们选择把windows上的字体复制到ubuntu上来安装的方法,步骤如下: 从win ...
- 一个不错的工具推荐:JMeter
在开发中可能会遇到一些场景需要对程序的性能,并发能力等进行度量,就是对一些程序的性能进行度量,生成一些报告等,最近遇到了一个不错的工具 apache JMeter,它是用java的swing开发的,功 ...
- 基本类型算法题目学习(EPI)
1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...
- 3种方法教你PS快速去掉水印
方法一:使用选框工具 勾选水印部分: 按住Shift+f5选择内容识别: 然后 ctrl+d 取消选择,水印就去掉了 PS:其实这个方法有个快捷办法,直接使用选框工具选中之后,按Delete就可以弹出 ...
- 各浏览器Cookie大小、个数限制【转】
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- ubuntu16.04下安装ros-kinetic
参考:http://wiki.ros.org/kinetic/Installation/Ubuntu 1.添加ROS软件源 ~$ sudo sh -c 'echo "deb http://p ...