博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 甲级 1068 Find More Coins(0,1背包)
阅读量:5163 次
发布时间:2019-06-13

本文共 2430 字,大约阅读时间需要 8 分钟。

1068. Find More Coins (30)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 104 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 (<=104, the total number of coins) and M(<=102, 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 V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = 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 95 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 87 2 4 3
Sample Output 2:
No Solution
0,1背包
按照字典序最小的输出方案,先把物品从大到小排序,然后再背包,就可以保证选的是字典序最小的,记录路径用二维数组
 
#include 
#include
#include
#include
#include
#include
using namespace std;int n,m;const int maxn=1e4;int a[maxn+5];int dp[105];int s[maxn+5][105];void fun(int x,int y){ if(x>n) return; if(s[x][y]==1) { if(y-a[x]==0) printf("%d\n",a[x]); else printf("%d ",a[x]); fun(x+1,y-a[x]); } else fun(x+1,y); }int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); memset(dp,-1,sizeof(dp)); memset(s,0,sizeof(s)); dp[0]=0; s[0][0]=-1; for(int i=n;i>=1;i--) { for(int j=m;j>=a[i];j--) { if(dp[j-a[i]]!=-1) { dp[j]=dp[j-a[i]]+a[i]; s[i][j]=1; } } } if(dp[m]==-1) printf("No Solution\n"); else fun(1,m); return 0;}

转载于:https://www.cnblogs.com/dacc123/p/8228591.html

你可能感兴趣的文章
IOC及AOP实现原理
查看>>
CocoaPods安装和使用教程
查看>>
WordPress博客搭建与问题总结
查看>>
C#中 property 与 attribute的区别
查看>>
POJ 1988 Cube Stacking(带权并查集)
查看>>
VMware vSphere虚拟化-VMware ESXi 5.5组件安装过程记录
查看>>
HDU 4960 Handling the past 2014 多校9 线段树
查看>>
时间管理-未经思考的人生不值得过
查看>>
cf 749D Leaving Auction
查看>>
[习题]验证控件(Validator)搭配 当地语系(Localization)
查看>>
XidianOJ 1213 小V的滑板鞋
查看>>
2017-2018-1 20155313 《信息安全系统设计基础》第八周课下作业2
查看>>
nginx的缓存设置提高性能
查看>>
C基础--单链表的构造
查看>>
光标定位、模糊查询
查看>>
获取Android控件ListView中被选中的某一列的值
查看>>
UVA 11374 Airport Express 机场快线 Dijistra+路径
查看>>
UIWebView 无缝切换到 WKWebView
查看>>
【python小练】0002
查看>>
【Django】不知道为什么就是想学一下 01
查看>>